使用pyexiv2将EXIF信息从一张图片复制到另一张图片
我正在尝试使用pyexiv2将一张图片的EXIF信息复制到它的缩略图上。在寻找解决方案时,我在Stack Overflow上发现了一篇帖子。看起来帖子中使用的API已经过时,最新版本中找不到了。根据最新的文档,我创建了一个这样的函数:
def get_exif(file):
"""
Retrieves EXIF information from a image
"""
ret = {}
metadata = pyexiv2.ImageMetadata(str(file))
metadata.read()
info = metadata.exif_keys
for key in info:
data = metadata[key]
ret[key] = data.raw_value
return ret
def write_exif(originFile, destinationFile, **kwargs):
"""
This function would write an exif information of an image file to another image file
"""
exifInformation = get_exif(originFile)
metadata = pyexiv2.ImageMetadata(str(destinationFile))
for key, value in exifInformation.iteritems():
metadata[key] = value
copyrightName = kwargs.get('copyright', None)
if copyrightName != None:
metadata['Exif.Image.Copyright'] = copyrightName
try:
metadata.write()
except:
return False
else:
return True
但是这段代码出现了错误:
Python的参数类型在 _ExifTag._setParentImage(_ExifTag, NoneType)与C++的签名不匹配: _setParentImage(exiv2wrapper::ExifTag {lvalue}, exiv2wrapper::Image {lvalue})
我现在完全不知道哪里出了问题。有人能帮我一下吗?谢谢!
编辑
根据@unutbu的建议,我把get_exif()方法中的data.raw_value改成了data.value,但仍然遇到同样的问题。我在这里贴出更多关于这个错误的信息:
Python argument types in
_ExifTag._setParentImage(_ExifTag, NoneType)
did not match C++ signature:
_setParentImage(exiv2wrapper::ExifTag {lvalue}, exiv2wrapper::Image {lvalue})
Request Method: POST
Request URL: http://localhost:8000/accounts/photography/
Django Version: 1.3.1
Exception Type: ArgumentError
Exception Value:
Python argument types in
_ExifTag._setParentImage(_ExifTag, NoneType)
did not match C++ signature:
_setParentImage(exiv2wrapper::ExifTag {lvalue}, exiv2wrapper::Image {lvalue})
Exception Location: /usr/lib64/python2.7/site-packages/pyexiv2/exif.py in _set_owner, line 107
**Traceback**
/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
response = callback(request, *callback_args, **callback_kwargs) ...
/usr/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py in _wrapped_view
return view_func(request, *args, **kwargs) ...
/home/swaroop/public_html/xyz/xyz/apps/photography/views.py in accountsPhotoList
write_exif(originFile=filePath, destinationFile=output) ...
/home/swaroop/public_html/xyz/xyz/library/imageManipulation.py in write_exif
metadata[key] = value ...
/usr/lib64/python2.7/site-packages/pyexiv2/metadata.py in __setitem__
return getattr(self, '_set_%s_tag' % family)(key, tag_or_value) ...
/usr/lib64/python2.7/site-packages/pyexiv2/metadata.py in _set_exif_tag
tag._set_owner(self) ...
/usr/lib64/python2.7/site-packages/pyexiv2/exif.py in _set_owner
self._tag._setParentImage(metadata._image) ...
Python Executable: /usr/bin/python2.7
Python Version: 2.7.2
2 个回答
1
不要用 metadata['Exif.Image.Copyright'] = copyrightName
这种写法
你应该使用下面的语法:
metadata['Exif.Image.Copyright'] = pyexiv2.ExifTag('Exif.Image.Copyright', copyrightName)
注意:对于 "Exif.Image.Copyright",copyrightName 的值应该是字符串类型
完整示例:
import pyexiv2
metadata = pyexiv2.ImageMetadata(image_name)
metadata.read()
metadata.modified = True
metadata.writable = os.access(image_name ,os.W_OK)
metadata['Exif.Image.Copyright'] = pyexiv2.ExifTag('Exif.Image.Copyright', 'copyright@youtext')
metadata.write()
希望这能对你有所帮助
3
在pyexiv2的0.3版本中,@user2431382提供的解决方案不能把EXIF标签写入到目标文件和源文件不一样的文件中。下面这个版本对我有效:
m1 = pyexiv2.ImageMetadata( source_filename )
m1.read()
# modify tags ...
# m1['Exif.Image.Key'] = pyexiv2.ExifTag('Exif.Image.Key', 'value')
m1.modified = True # not sure what this is good for
m2 = pyexiv2.metadata.ImageMetadata( destination_filename )
m2.read() # yes, we need to read the old stuff before we can overwrite it
m1.copy( m2 )
m2.write()