Jis*_*son 6 python python-imaging-library pyexiv2
当我尝试使用 PIL 调整图像大小(缩略图)时,它会破坏与图像关联的 exif 数据,我该如何保存它。
我调整图像大小并将其作为图像缓冲区上传到云端。
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())
Run Code Online (Sandbox Code Playgroud)
调整大小后的图像没有 exif 数据。
注意:我自己还没有这样做过,但据我所知,PIL 只允许读取 exif 标签,但不能写入它们。您可能需要 gexiv2 或 pyexiv2 将标签写入缩略图。
更新:我很好奇并亲自尝试过:D 如果我没猜错的话,您只想复制元数据而不进行进一步修改。
这仍然很粗糙,但似乎有效:
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
Run Code Online (Sandbox Code Playgroud)
顺便说一句:谢谢你提出这个有趣的问题!