使用pyexiv2将EXIF信息从一个图像复制并写入另一个图像

时间:2012-01-07 13:44:45

标签: python django

尝试使用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中的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 个答案:

答案 0 :(得分:2)

使用pyexiv2版本0.3,@ user2431382的解决方案将不允许将EXIF标记写入destination_file!= source_file。以下版本适用于我:

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

答案 1 :(得分:1)

而不是metadata['Exif.Image.Copyright'] = copyrightName

您必须使用语法

metadata['Exif.Image.Copyright']  = pyexiv2.ExifTag('Exif.Image.Copyright', copyrightName)

注意:copyrightName值应为“Exif.Image.Copyright”

的字符串

完整示例

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

希望这可以帮到你