Pillow
Pillow copied to clipboard
getexif().tobytes() throws 'bytes' object has no attribute 'encode'
This also indirectly breaks PIL.ImageOps.exif_transpose().
What did you do?
PIL.Image.open("x.jpg").getexif().tobytes()
The image is attached:
What actually happened?
throws:
File .../.venv/lib/python3.10/site-packages/PIL/Image.py:3589, in Exif.tobytes(self, offset)
3587 value[0xA005] = self.get_ifd(0xA005)
3588 ifd[tag] = value
-> 3589 return b"Exif\x00\x00" + head + ifd.tobytes(offset)
File .../.venv/lib/python3.10/site-packages/PIL/TiffImagePlugin.py:876, in ImageFileDirectory_v2.tobytes(self, offset)
874 else:
875 values = value if isinstance(value, tuple) else (value,)
--> 876 data = self._write_dispatch[typ](self, *values)
878 tagname = TiffTags.lookup(tag, self.group).name
879 typname = "ifd" if is_ifd else TYPES.get(typ, "unknown")
File .../.venv/lib/python3.10/site-packages/PIL/TiffImagePlugin.py:730, in ImageFileDirectory_v2.write_string(self, value)
727 @_register_writer(2)
728 def write_string(self, value):
729 # remerge of https://github.com/python-pillow/Pillow/pull/1416
--> 730 return b"" + value.encode("ascii", "replace") + b"\0"
AttributeError: 'bytes' object has no attribute 'encode'
What are your OS, Python and Pillow versions?
- OS: Arch Linux
- Python: 3.10.5
- Pillow: 9.2.0
https://www.awaresystems.be/imaging/tiff/tifftags/make.html states that tag 271 should have type ASCII, but in the image you've provided, it has the type UNDEFINED. So that is what is unusual about your image.
I've created PR #6493 to resolve this.
Thank you! The image was created in the Android emulator Camera btw. I've not encountered this case somewhere else