5

when I try to resize(thumbnail) an image using PIL , it destroys the exif data associated with the image, How can I save it.

I resize the image and upload it to the cloud as image buffer.

file_path = '...'
file_name = '...'
im = Image.open( file_path )
size =(512,521)
im.thumbnail( size, Image.ANTIALIAS)
thumbnail_buf_string = StringIO.StringIO()
file_save_extension = 'JPEG'
im.save(thumbnail_buf_string, format=file_save_extension)
upload_to_cloud('512_' + file_name , thumbnail_buf_string.getvalue())

The resized image has no exif data.

Jisson
  • 3,566
  • 8
  • 38
  • 71

1 Answers1

5

Note: I haven't done this myself yet, but to my knowledge, PIL only allows to read exif tags but cannot write them. You will probably need gexiv2 or pyexiv2 to write the tags to your thumbnails.

UPDATE: I got curious and tried it myself :D If i got you right, you just want to copy the metadata without further modifications.

This is still crude but seems to work:

import os
import Image
import pyexiv2

fp = '/home/klaus/workspace'
fn = 'img_2380.jpg'

full_path = os.path.join(fp, fn)
print full_path

im = Image.open(full_path)
size = 512, 512
im.thumbnail(size, Image.ANTIALIAS)
im.save('bla.jpg', 'JPEG')

oldmeta = pyexiv2.ImageMetadata(full_path)
oldmeta.read()
# read metadata of the original file

newmeta = pyexiv2.ImageMetadata('bla.jpg')
newmeta.read()
# read metadata of the new file
# yes, there aren't any, but this is crucial!
# you need this class as the target for copying!

oldmeta.copy(newmeta)

newmeta.write()
# don't forget to write the data to the new file

BTW: Thanks for the interesting question!

Klaus-Dieter Warzecha
  • 2,265
  • 2
  • 27
  • 33
  • 1
    sounds true, I tried pyexiv2 http://stackoverflow.com/a/17059383/658976, But I need to use the imagebuffer. – Jisson Jun 12 '13 at 08:59
  • I installed gexiv2, and can edit exif data , but how can I edit edit exif of string image in memory. eg: im.save(thumbnail_buf_string, format=file_save_extension) – Jisson Jun 12 '13 at 10:46
  • You are wright, Is there any way to set the exif data of original image to the resized with out saving the second one in disc? , means setting exif to the image instance of resized image itself. If not possible I think the only way to do as you suggested , thank you – Jisson Jun 13 '13 at 05:39
  • You're right that it would be nice to do all that in memory. Unfortunately, i haven't found out yet if that's possible or how to achieve it. – Klaus-Dieter Warzecha Jun 13 '13 at 06:18