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

TypeError on getexif().tobytes() #6737

Closed
deepio opened this issue Nov 13, 2022 · 8 comments · Fixed by #6740
Closed

TypeError on getexif().tobytes() #6737

deepio opened this issue Nov 13, 2022 · 8 comments · Fixed by #6740
Labels

Comments

@deepio
Copy link

deepio commented Nov 13, 2022

What did you do?

Saved a tiff with same exif.

What did you expect to happen?

saved file

What actually happened?

Traceback (most recent call last):
  File "/Users/deepio/Desktop/test/ape.py", line 4, in <module>
    im.save("testing.tiff", format=im.format, exif=im.getexif().tobytes())
  File "/Users/deepio/.pyenv/versions/3.9.13/lib/python3.9/site-packages/PIL/Image.py", line 2353, in save
    save_handler(self, fp, filename)
  File "/Users/deepio/.pyenv/versions/3.9.13/lib/python3.9/site-packages/PIL/JpegImagePlugin.py", line 747, in _save
    exif = exif.tobytes()
  File "/Users/deepio/.pyenv/versions/3.9.13/lib/python3.9/site-packages/PIL/Image.py", line 3628, in tobytes
    return b"Exif\x00\x00" + head + ifd.tobytes(offset)
  File "/Users/deepio/.pyenv/versions/3.9.13/lib/python3.9/site-packages/PIL/TiffImagePlugin.py", line 878, in tobytes
    data = ifd.tobytes(offset)
  File "/Users/deepio/.pyenv/versions/3.9.13/lib/python3.9/site-packages/PIL/TiffImagePlugin.py", line 887, in tobytes
    "<table: %d bytes>" % len(data) if len(data) >= 16 else str(values)
TypeError: object of type 'int' has no len()

So I traced it:

> /Users/deepio/Desktop/ape.py(11)go()
-> print(im.getexif().tobytes())
(Pdb) b /Users/deepio/.pyenv/versions/3.9.1/lib/python3.9/site-packages/PIL/TiffImagePlugin.py:864
Breakpoint 1 at /Users/deepio/.pyenv/versions/3.9.1/lib/python3.9/site-packages/PIL/TiffImagePlugin.py:864
(Pdb) condition 1 tag==5
New condition set for breakpoint 1.
(Pdb) commands 1
(com) print(f"tag: {tag}\nvalue: {value}")
(com) end
(Pdb) c
tag: 5
value: 0
> /Users/deepio/.pyenv/versions/3.9.1/lib/python3.9/site-packages/PIL/TiffImagePlugin.py(864)tobytes()
-> if tag == STRIPOFFSETS:

(Pdb) is_ifd
False
(Pdb) values = value if isinstance(value, tuple) else (value,)
(Pdb) values
(0,)
(Pdb) self._write_dispatch[typ](self, *values)
0

<same traceback as above>

So a possible fix could be to do the same isinstance on data as it is done on values above it.

What are your OS, Python and Pillow versions?

  • OS: recreated on MacOS 12.5.1 / Ubuntu 18.04
  • Python: (3.9.1/3.9.13)/3.9.9
  • Pillow: 9.3.0
from PIL import Image

im = Image.open("38bfc38a_a51a_c735def5ae62.tiff")
# saving is not required to trigger the same failure.
im.getexif().tobytes()
@deepio
Copy link
Author

deepio commented Nov 13, 2022

Possibly a type of regression with #1524 but it's not with saving

@radarhere
Copy link
Member

Would you be able to upload a copy of the image?

@deepio
Copy link
Author

deepio commented Nov 14, 2022

Here we go:
t56023

@deepio deepio changed the title Tiff TypeError on getexif() TypeError on getexif() Nov 14, 2022
@deepio deepio changed the title TypeError on getexif() TypeError on getexif().tobytes() Nov 14, 2022
@radarhere
Copy link
Member

Thanks. I've created PR #6740 to resolve this.

hugovk added a commit that referenced this issue Nov 15, 2022
@huynd26
Copy link

huynd26 commented Dec 28, 2022

seem the error still appears although I updated to version 9.3.0

    transposed_image.info["exif"] = transposed_exif.tobytes()
  File "PIL/Image.py", line 3628, in tobytes
    return b"Exif\x00\x00" + head + ifd.tobytes(offset)
  File "PIL/TiffImagePlugin.py", line 878, in tobytes
    data = ifd.tobytes(offset)
  File "PIL/TiffImagePlugin.py", line 887, in tobytes
    "<table: %d bytes>" % len(data) if len(data) >= 16 else str(values)

@radarhere
Copy link
Member

PR #6740 will be part of Pillow 9.4.0, due out on January 2.

If you do have the same error, it should be fixed then.

@huynd26
Copy link

huynd26 commented Dec 28, 2022

Thank you, expect to be released soon

@radarhere
Copy link
Member

Pillow 9.4.0 has now been released.

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.

3 participants