4

Am trying to copy EXIF information of a Image file to the resized version of the same image using pyexiv2. Searching for a solution i stumbled upon a post in Stack Overflow. It looks like the api's used in the function is outdated and is not available in the latest version. Based on the latest document i created a function like this

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

But this ends up in an error

Python argument types in _ExifTag._setParentImage(_ExifTag, NoneType) did not match C++ signature: _setParentImage(exiv2wrapper::ExifTag {lvalue}, exiv2wrapper::Image {lvalue})

I am now clueless on what went wrong. Could someone please help me out? Thanks

EDIT

Based on @unutbu 's suggestion I made change to data.raw_value in get_exif() method to data.value but still am facing the same issue. Am pasting more information regarding this error:

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
Community
  • 1
  • 1
swaroop
  • 125
  • 2
  • 8
  • Well figured out that metadata.read() should be added to write_exif. Hope this would help someone who too faced similar issue – swaroop Jan 09 '12 at 15:37

2 Answers2

3

With pyexiv2 version 0.3, the solution of @user2431382 will not allow to write the EXIF tags to destination_file != source_file. The following version works for me:

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()
Joachim W
  • 7,290
  • 5
  • 31
  • 59
1

Instead of metadata['Exif.Image.Copyright'] = copyrightName

You have to use syntax as

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

Note: copyrightName value should be string for "Exif.Image.Copyright"

Full example

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

hope this may help you

Ruwa
  • 11
  • 1