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

Saving binary tiff files with tiff_deflate compression fails under certain conditions #7892

Closed
eroux opened this issue Mar 22, 2024 · 2 comments · Fixed by #7893
Closed

Saving binary tiff files with tiff_deflate compression fails under certain conditions #7892

eroux opened this issue Mar 22, 2024 · 2 comments · Fixed by #7893
Labels

Comments

@eroux
Copy link

eroux commented Mar 22, 2024

What did you do?

I have this file:

20210415185447038_0158.zip

(you'll have to unzip it to test, I zipped it so that I could upload it in this issue)

I'm running the following script:

from PIL import Image

img = Image.open("20210415185447038_0158.tiff")
# any of these operations make the final line work
#img = img.convert("L")
#img = img.rotate(180).rotate(180)
# removing the compression argument also makes it work
img.save("test.tiff", compression="tiff_deflate")

What did you expect to happen?

I expected the file to be saved

What actually happened?

I got

$ python3 convert_tiff.py 
_TIFFVSetField: toto2.tiff: Ignored tag "OldSubfileType" (not supported by libtiff).
Traceback (most recent call last):
  File "/home/admin/tmp/convert_tiff.py", line 8, in <module>
    img.save("toto2.tiff", compression="tiff_deflate")
  File "/home/admin/.local/lib/python3.9/site-packages/PIL/Image.py", line 2439, in save
    save_handler(self, fp, filename)
  File "/home/admin/.local/lib/python3.9/site-packages/PIL/TiffImagePlugin.py", line 1838, in _save
    e = Image._getencoder(im.mode, "libtiff", a, encoderconfig)
  File "/home/admin/.local/lib/python3.9/site-packages/PIL/Image.py", line 420, in _getencoder
    return encoder(mode, *args + extra)
RuntimeError: Error setting from dictionary

What are your OS, Python and Pillow versions?

  • OS: Debian 5.10
  • Python: 3.9.2
  • Pillow: 10.2.0

The strange thing is that if I do anything with the image then saving works, or if I save it uncompressed it works too, it only fails when I save the untouched image.

@radarhere
Copy link
Member

The problem is TIFF Tag 255, SubfileType.

Deleting the tag will cause the code to work.

from PIL import Image
img = Image.open("20210415185447038_0158.tiff")
del img.tag_v2[255]
img.save("test.tiff", compression="tiff_deflate")

When you're converting or rotating the image, you're copying it, and when it is copied, it loses the TIFF tags.

I've created #7893 to fix this by automatically deleting the tag when using compression.

@eroux
Copy link
Author

eroux commented Mar 22, 2024

thanks a lot!

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