My photo editor screwed up the tags for many of my photos. I took this as the final push to start getting familiar with Python and write a script to clean the tags. I found out that pyexiv2 might come in quite handy and I tried to basically follow the tutorial (http://tilloy.net/dev/pyexiv2/tutorial.html).
What I am trying to do:
- Parse the original data from the XMP file (NB: I use sidecar files instead of writing metadata into the photo files directly).
- Read the original tag(s) from Xmp.dc.subject (and print them to stdout for debugging)
- Change the tag to a new value (and for debugging print the new value)
- Write the new data back to the XMP file
Things look quite nice until I check the XMP file in an editor. The changed value just doesn't show up. However the XMP file gets a new timestamp and if I make the XMP file read-only, I get an error message. So it seems that something gets written to file. Just not the correct data.
Here is a minimal script that shows my problem:
import pyexiv2
key= 'Xmp.dc.subject'
metadata = pyexiv2.ImageMetadata('test.xmp')
metadata.read()
print metadata[key]
metadata[key] = ['NewTag']
print metadata[key]
metadata.write()
Its output is:
<Xmp.dc.subject [bag Text] = ['OldTag']>
<Xmp.dc.subject [bag Text] = ['NewTag']>
Script terminated.
But the relevant section of the XMP file still looks like this:
<dc:subject>
<rdf:Bag>
<rdf:li>OldTag</rdf:li>
</rdf:Bag>
</dc:subject>
So what do I have to do in order to write the changed metadata to the XMP file?
Please note that I am a complete beginner to Python (but not to programming), so the issue might result from something trivial that I am not aware of. I am running Python 2.7.6 on Debian Linux.
The API documentation about pyexiv2.metadata.ImageMetadata says:
It looks like sidecar files may not be supported for writing.