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

Conversion of TIFF with CCITT Group 4 Fax Encoding sometimes crashes under Windows #5119

Closed
jnik-aarhus opened this issue Dec 21, 2020 · 2 comments · Fixed by #5120
Closed
Labels

Comments

@jnik-aarhus
Copy link

jnik-aarhus commented Dec 21, 2020

What did you do?

I'm trying to convert various TIFF files to TIFF with LZW compression. I have now repeatedly run into TIFF files with CCITT Group 4 Fax Encoding which will make Python crash under Windows.

What did you expect to happen?

I expect an exception to be raised from the PIL TIFFImagePlugin, instead of a hard crash.

What actually happened?

On Windows, the code below with the TIFF file provided will crash the entire Python process without raising exceptions. Under WSL, an OSError is raised, however:

saving image
TIFFWriteDirectoryTagSubifd: Illegal value for SubIFD tag.
TIFFWriteDirectoryTagSubifd: Illegal value for SubIFD tag.
Traceback (most recent call last):
  File "main.py", line 33, in <module>
    img2tif(par_dir / "fail.tif", par_dir / "out")
  File "main.py", line 27, in img2tif
    im.save(str(img_out), compression="tiff_lzw")  # Python crashes here
  File "/home/jnik/.local/lib/python3.8/site-packages/PIL/Image.py", line 2151, in save
    save_handler(self, fp, filename)
  File "/home/jnik/.local/lib/python3.8/site-packages/PIL/TiffImagePlugin.py", line 1631, in _save
    raise OSError(f"encoder error {s} when writing image file")
OSError: encoder error -9 when writing image file

What are your OS, Python and Pillow versions?

  • OS: Windows 10 Pro 2004, 19041.685 and WSL Debian (Linux 4.19.128-microsoft-standard #1 SMP Tue Jun 23 12:58:10 UTC 2020 x86_64 GNU/Linux)
  • Python: Tested under 3.8.5 and 3.9.1
  • Pillow: 8.0.1

Code and further information

# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------
from pathlib import Path

from PIL import Image


# -----------------------------------------------------------------------------
# Image conversion
# -----------------------------------------------------------------------------


def img2tif(img_file: Path, out_dir: Path) -> None:
    img_out: Path = out_dir / img_file.name
    img_out = img_out.with_suffix(".tif")
    print(img_out)
    print("opening image")
    im = Image.open(img_file)
    print("loading image")
    im.load()
    print("saving image")
    im.save(str(img_out), compression="tiff_lzw")  # Python crashes here
    print("saved image")  # Never reached


if __name__ == "__main__":
    par_dir = Path(__file__).parent
    img2tif(par_dir / "fail.tif", par_dir / "out")

I have attached both the code and the offending TIFF image here: tif_fail.zip
Note that opening the offending file in e.g. IrfanView and saving it again will fix the issue. So the TIFF file is definitely damaged somehow. I don't expect Pillow to handle the file, but rather fail more gracefully.

@radarhere radarhere added the TIFF label Dec 21, 2020
@radarhere radarhere changed the title Conversion of TIFF with CCITT Group 4 Fax Encoding sometimes crashes Python under Windows Conversion of TIFF with CCITT Group 4 Fax Encoding sometimes crashes under Windows Dec 21, 2020
@radarhere
Copy link
Member

Thanks for reporting! I've created PR #5120 as a possible solution.

@jnik-aarhus
Copy link
Author

Thank you so much for the quick response!

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