Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

getexif().tobytes() throws 'bytes' object has no attribute 'encode'

Open phiresky opened this issue 3 years ago • 2 comments

This also indirectly breaks PIL.ImageOps.exif_transpose().

What did you do?

PIL.Image.open("x.jpg").getexif().tobytes()

The image is attached:

x.jpg

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

phiresky avatar Aug 11 '22 09:08 phiresky

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.

radarhere avatar Aug 11 '22 10:08 radarhere

Thank you! The image was created in the Android emulator Camera btw. I've not encountered this case somewhere else

phiresky avatar Aug 11 '22 11:08 phiresky