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 TIFF as JPEG fails with OSError: encoder error -2 #6932

Closed
gverm opened this issue Feb 9, 2023 · 6 comments · Fixed by #6939
Closed

Saving TIFF as JPEG fails with OSError: encoder error -2 #6932

gverm opened this issue Feb 9, 2023 · 6 comments · Fixed by #6939

Comments

@gverm
Copy link

gverm commented Feb 9, 2023

What did you do?

Tried to convert TIFF to JPEG.

What did you expect to happen?

Converted image saved successfully.

What actually happened?

Bogus marker length
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/gverm/Desktop/venv/lib/python3.10/site-packages/PIL/Image.py", line 2430, in save
    save_handler(self, fp, filename)
  File "/home/gverm/Desktop/venv/lib/python3.10/site-packages/PIL/JpegImagePlugin.py", line 803, in _save
    ImageFile._save(im, fp, [("jpeg", (0, 0) + im.size, 0, rawmode)], bufsize)
  File "/home/gverm/Desktop/venv/lib/python3.10/site-packages/PIL/ImageFile.py", line 517, in _save
    _encode_tile(im, fp, tile, bufsize, fh)
  File "/home/gverm/Desktop/venv/lib/python3.10/site-packages/PIL/ImageFile.py", line 547, in _encode_tile
    raise OSError(msg) from exc
OSError: encoder error -2 when writing image file

What are your OS, Python and Pillow versions?

  • OS: EndeavourOS
  • Python: 3.10 & 3.11
  • Pillow: 9.4.0 & current git
from PIL import Image, ImageOps, TiffImagePlugin
src_img = Image.open("/home/gverm/ae213421281f00a1cbe6fa2a9eb9cea5-A65_02_048$02.tif")
exif = src_img.getexif()
del exif[TiffImagePlugin.STRIPOFFSETS]
dst_img = ImageOps.exif_transpose(src_img).convert("RGB")
dst_img.save("test.jpg", quality=95, exif=exif)

Link to image

@radarhere
Copy link
Member

I find that if you add in

del exif[TiffImagePlugin.ICCPROFILE]

the error disappears.

@radarhere radarhere changed the title Converting TIFF to JPEG fails with OSError: encoder error -2 when writing image file Saving TIFF as JPEG fails with OSError: encoder error -2 Feb 10, 2023
@gverm
Copy link
Author

gverm commented Feb 10, 2023

@radarhere Is this something that is to be expected or is there actually a bug here?

@radarhere
Copy link
Member

Without that workaround, the EXIF data you're trying to save is 417170 bytes long.

https://en.wikipedia.org/wiki/Exif

Exif metadata are restricted in size to 64 kB in JPEG images because according to the specification this information must be contained within a single JPEG APP1 segment.

So it is a restriction of the JPEG format.

@gverm
Copy link
Author

gverm commented Feb 10, 2023

Thanks for the explanation. Would it be possible to catch this case and display an error stating that this limitation has been hit?

@radarhere
Copy link
Member

I've created PR #6939 to do so.

hugovk added a commit that referenced this issue Feb 11, 2023
@fiksii-copilot
Copy link

Downgrade to 10.0.1 helped me

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

Successfully merging a pull request may close this issue.

3 participants