Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
phiresky opened this issue Aug 11, 2022 · 2 comments · Fixed by #6493
Closed

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

phiresky opened this issue Aug 11, 2022 · 2 comments · Fixed by #6493
Labels

Comments

@phiresky
Copy link

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
@radarhere
Copy link
Member

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 radarhere added the Exif label Aug 11, 2022
@phiresky
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants