26

When I try to resize (thumbnail) an image using PIL, the exif data is lost.

What do I have to do preserve exif data in the thumbnail image? When I searched for the same, got some links but none seem to be working.

from PIL import  Image
import StringIO

file_path = '/home/me/img/a.JPG'
im = Image.open( file_path)
THUMB_SIZES = [(512, 512)]
for thumbnail_size in THUMB_SIZES:
    im.thumbnail( thumbnail_size, Image.ANTIALIAS)
    thumbnail_buf_string = StringIO.StringIO()
    im.save('512_' + "a", "JPEG")

The orginal image has exif data, but image im(512_a.JPEG) doesn't.

Gary Kerr
  • 13,650
  • 4
  • 48
  • 51
Jisson
  • 3,566
  • 8
  • 38
  • 71

3 Answers3

23

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)
plowman
  • 13,335
  • 8
  • 53
  • 53
Gary Kerr
  • 13,650
  • 4
  • 48
  • 51
  • I tried it but im.save('P4072956_thumb.jpg', exif=exif) not saving the exif data – Jisson Jun 11 '13 at 16:09
  • Try `print exif` to confirm that there is exif data. Or you can try running the second example to view the exif data. – Gary Kerr Jun 11 '13 at 16:23
  • When I open the orginal file with 'Phatch Image Inspector' ,there is exif data,But when I open the resized image file ,with the above tool, there no exif – Jisson Jun 11 '13 at 16:46
  • What happens when you run the second code example with the original image and thumbnail? – Gary Kerr Jun 11 '13 at 16:56
  • Both orginal and resized image prints the same dict! – Jisson Jun 11 '13 at 17:06
  • both image has same exif printed even though I didn't pass exif to save function. – Jisson Jun 12 '13 at 05:40
  • 1
    I got the solution,import pyexiv2 metadata = pyexiv2.ImageMetadata(file_path) metadata.read() thumb = metadata.exif_thumbnail thumb.set_from_file(file_path) thumb.write_to_file('512_' + "a") thumb.erase() metadata.write() – Jisson Jun 12 '13 at 06:06
  • 1
    But ImageLength and ImageWidth are part of the exif data; won't they now be incorrect? – jMyles Jul 12 '15 at 15:28
  • `im.save('P4072956_thumb.jpg', exif=exif)` does not write the `exif` information – 8bitjunkie Aug 28 '15 at 21:24
21

In my project, i met the same issue with you. After searching Google, I found piexif library. It help to Pilow save exif data to thumbnails.

You can use the source code below:

from PIL import  Image
import piexif
import StringIO


file_path = '/home/me/img/a.JPG'
im = Image.open( file_path)

# load exif data
exif_dict = piexif.load(im.info["exif"])
exif_bytes = piexif.dump(exif_dict)

THUMB_SIZES = [(512, 512)]
for thumbnail_size in THUMB_SIZES:
    im.thumbnail( thumbnail_size, Image.ANTIALIAS)
    thumbnail_buf_string = StringIO.StringIO()
    # save thumbnail with exif data
    im.save('512_' + "a", "JPEG", exif=exif_bytes)

Note: I am using python 3.4 and ubuntu 14.04

Nguyen Sy Thanh Son
  • 5,300
  • 1
  • 23
  • 33
2
import pyexiv2
from PIL import  Image

file_path = '/home/../img/a.JPG'
metadata = pyexiv2.ImageMetadata(file_path)
metadata.read()
thumb = metadata.exif_thumbnail
thumb.set_from_file(file_path)
thumb.write_to_file('512_' + "a")
thumb.erase()
metadata.write()

Now I open the image using (Patch Image Inspector) , I can see the exif data

Jisson
  • 3,566
  • 8
  • 38
  • 71