Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

Python: Remove Exif info from images

In order to reduce the size of images to be used in a website, I reduced the quality to 80-85%. This decreases the image size quite a bit, up to an extent.

To reduce the size further without compromising the quality, my friend pointed out that raw images from cameras have a lot of metadata called Exif info. Since there is no need to retain this Exif info for images in a website, we can remove it. This will further reduce the size by 3-10 kB.

But I'm not able to find an appropriate library to do this in my Python code. I have browsed through related questions and tried out some of the methods:

Original image: http://mdb.ibcdn.com/8snmhp4sjd75vdr27gbadolc003i.jpg

  1. Mogrify

    /usr/local/bin/mogrify -strip filename
    

    Result: http://s23.postimg.org/aeaw5x7ez/8snmhp4sjd75vdr27gbadolc003i_mogrify.jpg This method reduces the size from 105 kB to 99.6 kB, but also changed the color quality.

  2. Exif-tool

    exiftool -all= filename
    

    Result: http://s22.postimg.org/aiq99o775/8snmhp4sjd75vdr27gbadolc003i_exiftool.jpg This method reduces the size from 105 kB to 72.7 kB, but also changed the color quality.

  3. This answer explains in detail how to manipulate the Exif info, but how do I use it to remove the info?

Can anyone please help me remove all the extra metadata without changing the colours, dimensions, and other properties of an image?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
from PIL import Image

image = Image.open('image_file.jpeg')

# next 3 lines strip exif
data = list(image.getdata())
image_without_exif = Image.new(image.mode, image.size)
image_without_exif.putdata(data)

image_without_exif.save('image_file_without_exif.jpeg')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...