Copying and Writing EXIF information from one image to another

Asked by swaroop Shankar V

Am trying to copy EXIF information of a Image file to the resized version of the same image using pyexiv2. 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})
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
Python Executable: /usr/bin/python2.7
Python Version: 2.7.2

**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/twiching/twiching/apps/photography/views.py in accountsPhotoList
            write_exif(originFile=filePath, destinationFile=output) ...

/home/swaroop/public_html/twiching/twiching/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) ...

Please help me to understand what went wrong. Thanks

Question information

Language:
English Edit question
Status:
Solved
For:
pyexiv2 Edit question
Assignee:
No assignee Edit question
Solved by:
Olivier Tilloy
Solved:
Last query:
Last reply:
Revision history for this message
Best Olivier Tilloy (osomon) said :
#1

I think the problem is in write_exif(…), where you forgot to call metadata.read() before assigning values.

In any case, it looks like what you are trying to accomplish can be implemented in a much simpler way using ImageMetadata.copy(…). Have a look at the API documentation at http://tilloy.net/dev/pyexiv2/api.html#pyexiv2.metadata.ImageMetadata.copy. I hope this helps!

Revision history for this message
swaroop Shankar V (swaroopsv) said :
#2

Thanks Olivier, as you pointed out metadata.read() was the issue. Now its fixed and thanks for letting me know about the copy() API. Cheers :)

Revision history for this message
swaroop Shankar V (swaroopsv) said :
#3

Thanks Olivier Tilloy, that solved my question.