Search code examples
python-3.ximagexmppyexiv2

Editing xmp tags pyexiv2 modify_xmp does not replace xmp tags correctly


I am trying to write a script that will loop through a large number of images and write new xmp tags based on certain criteria.

I am using pyexiv2 to read and modify the 'Xmp.dc.subject' tag.

I am able to assign a new set of tags to the image, and using pyexiv2.read_xmp() to check my results shows that the new set of tags has replaced the old set of tags, as expected. However, when I check the properties in windows explorer or another photo manager, the old tags remain in addition to the new set of tags.

see my code below

from pyexiv2 import Image

path=some_path
img=Image(path)
tags=img.read_xmp()
tags.get('Xmp.dc.subject')  ####outputs list of tags ['old_tag1', 'old_tag2', 'old_tag3']

newtags=['new_tag1','new_tag2']

dict1={'Xmp.dc.subject':newtags}
img.modify_xmp(dict1)
img.close()

Now, when I open properties of the file in Explorer, or check in a photo manager software, the tags on the file are ['old_tag1', 'old_tag2', 'old_tag3','new_tag1','new_tag2']. The expected behaviour as stated in the pyexiv2 tutorial is that the new list of tags will replace the old tags.

I have tried using py3exiv2, but I am having problems with that library due to an error referencing Microsoft Visual Studio. Is there a way to achieve my outcome ideally using pyexiv2, or alternatively using any other method?


Solution

  • I found the solution to this problem. Windows explorer (and Adbobe Bridge, and i'm guessing other software too) displays both xmp tags and iptc tags.

    So if you modify the xmp tags only, explorer (or other software) will show the new xmp tags as well as the old iptc tags.

    So the solution is to use modify_xmp() and modify_iptc() to change both sets of tags.