如何使用PIL维护图像的exif数据调整大小 [英] how maintain exif data of images resizes using PIL

查看:173
本文介绍了如何使用PIL维护图像的exif数据调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用PIL调整图像的大小(缩略图)时,它会破坏与该图像相关联的exif数据, 我该如何保存.

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())

调整后的图像没有exif数据.

The resized image has no exif data.

推荐答案

注意:我自己还没有这样做,但是据我所知,PIL仅允许读取exif标记,但不能编写它们.您可能需要gexiv2或pyexiv2才能将标签写入缩略图.

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.

更新:我很好奇,并亲自尝试了:D 如果我说对了,您只想复制元数据而无需进一步修改.

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!

这篇关于如何使用PIL维护图像的exif数据调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆