How to preserve IPTC metadata of an image in python

When I try to resize (thumbnail) an image using PIL, the iptc data is lost.

What do I have to do preserve iptc data in the thumbnail image?

I saw there is a support of IptcImagePlugin in Pillow library but that's only for reading the iptc data of an image. How do I write the iptc data on an image after it is lost ?


Solution 1:

Maybe more of a work-around, but you can extract and insert IPTC data with both exiftool and ImageMagick.

That means you should be able to do it in Python with exiftool by adapting my answer here. Or by using wand which is a Python binding for ImageMagick. Failing that, I guess you could use Python subprocess module.


With exiftool, copy forward IPTC data from original.jpg to new.jpg:

exiftool -tagsfromfile original.jpg -IPTC:all new.jpg

In Python, that should look like this (untested):

import exiftool

# If  "exiftool" is not on your PATH, add the full path inside parentheses on next line
with exiftool.ExifTool() as et:
   et.execute(b"-tagsfromfile original.jpg -IPTC:all", b"new.jpg")

Another way of doing this is to extract the IPTC data to a file and then patch those values into your PIL-created thumbnail:

exiftool original.jpg -b -IPTC > saved.iptc
exiftool '-IPTC<=saved.iptc' new.jpg

With ImageMagick, extract IPTC to file extracted.iptc:

magick original.jpg extracted.iptc

And now insert into new.jpg:

magick new.jpg -profile extracted.iptc withprofile.jpg

Or, copying forward the IPTC from original.jpg into new.jpg to produce result.jpg just the same as above but in one fell swoop without creating a temporary file and relying on a bash shell "process substitution":

magick new.jpg -profile <(magick original.jpg iptc:-) result.jpg