getiptcinfo() fails to get iptc metadata for TIFF images
What did you do?
I'm trying to read IPTC metadata from a TIFF image. A simple example would be:
from PIL import Image
with Image.open(file_path) as im:
print(getiptcinfo(im))
What did you expect to happen?
The IPTC metadata output
What actually happened?
A TypeError saying a bytes-like object is required, not 'int'
What are your OS, Python and Pillow versions?
- OS: Windows 10
- Python: 3.12
- Pillow: 11.0.0
An image that reproduces the error can be found here: test_iptc_metadata.zip. The IPTC metadata in this image was created using both GIMP and Photoshop. However, neither application’s metadata is handled correctly in Pillow version 11.0.0.
This issue does not occur in Pillow version 10.4.0, where the metadata is processed as expected. The problem was likely introduced by a change made in the following line of code, which now returns an int type instead of a bytes type, leading to the error mentioned above.
Previously, in versions before 11.0.0, the line was:
data = im.tag.tagdata[TiffImagePlugin.IPTC_NAA_CHUNK]
Hmm. The change you're referring to comes from #8315. I tested the change against https://github.com/python-pillow/Pillow/blob/main/Tests/images/hopper.Lab.tif, and it works fine with this because that image specifies the tag type as UNDEFINED.
Your image specifies the tag type as LONG. Interestingly, according to https://web.archive.org/web/20240221164915/https://www.awaresystems.be/imaging/tiff/tifftags/iptc.html, this is a mistake that is often made.
I've created #8925
If you'd like an immediate fix, I find that
from PIL import Image, IptcImagePlugin, TiffImagePlugin
with Image.open("test_iptc_metadata.tif") as im:
im.tag_v2.tagtype[TiffImagePlugin.IPTC_NAA_CHUNK] = 7
print(IptcImagePlugin.getiptcinfo(im))
works.
@radarhere Thank you for your quick reply and the info.
That's interesting about the tag type; however, we have no control over it since the metadata comes from scanners or is added via Photoshop. :). Thank you for your PR fixing this issue.