Skip to content

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

@phiresky

Description

@phiresky

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions