how maintain exif data of images resizes using PIL

可紊 提交于 2019-12-03 16:29:56

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!