Preserve exif data of image with PIL when resize(create thumbnail)

I read throught some of the source code and found a way to make sure that the exif data is saved with the thumbnail.

When you open a jpg file in PIL, the Image object has an info attribute which is a dictionary. One of the keys is called exif and it has a value which is a byte string – the raw exif data from the image. You can pass this byte string to the save method and it should write the exif data to the new jpg file:

from PIL import Image

size = (512, 512)

im = Image.open('P4072956.jpg')
im.thumbnail(size, Image.ANTIALIAS)
exif = im.info['exif']
im.save('P4072956_thumb.jpg', exif=exif)

To get a human-readable version of the exif data you can do the following:

from PIL import Image
from PIL.ExifTags import TAGS

im = Image.open('P4072956.jpg')
for k, v in im._getexif().items():
    print TAGS.get(k, k), v

Leave a Comment