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

TIFF_LZW decompression is incorrect for 16bpc image #2839

Closed
giorgiosironi opened this issue Nov 7, 2017 · 1 comment
Closed

TIFF_LZW decompression is incorrect for 16bpc image #2839

giorgiosironi opened this issue Nov 7, 2017 · 1 comment
Labels
Bug Any unexpected behavior, until confirmed feature. TIFF

Comments

@giorgiosironi
Copy link

Pillow was refusing to open this TIFF in version 4.2.1. In version 4.3.0, it loads it, but then saving it in any format including TIFF produces a silent corruption. Python 2.7.14 in both cases.

This is the reproducing script:

from PIL import Image                                                                                                                      

# https://s3-external-1.amazonaws.com/prod-elife-published/articles/32330/elife-32330-fig1-v1.tif 
src_fp = 'elife-32330-fig1-v1.tif'
im = Image.open(src_fp)
target_fp = open('result.tif', 'w')
im.save(target_fp)

In 4.2.1:

  File "reproducing.py", line 5, in <module>
    im = Image.open(src_fp)
  File "/opt/loris/venv/local/lib/python2.7/site-packages/PIL/Image.py", line 2519, in open
    % (filename if filename else fp))
IOError: cannot identify image file 'elife-32330-fig1-v1.tif'

In 4.3.0, no output, file gets stored and corrupted.
elife-32330-fig1-v1.tif.gz
result.tif.gz
converting to PNG

I tried to track how the Image class changed from 4.2.1 to 4.3.0, but there were too many changes to identify the source. I don't expect it to start to work, but to continue throwing an exception if it can't be read safely.

@giorgiosironi giorgiosironi changed the title TIFF that could not be loaded gets corrupted by Pillow 4.3.0 TIFF that could not be loaded in 4.2.1 gets silently corrupted in 4.3.0 Nov 7, 2017
@wiredfool
Copy link
Member

If you use libtiff instead of the internal tiff decoder, it works:

from PIL import Image, TiffImagePlugin
TiffImagePlugin.READ_LIBTIFF=True

im = Image.open('issue_2839.tif')
im.show()

This image is a 16bit RGBA image, with tiff_lzw compression. Pillow 4.3.0 added support for the 16 bit RGBA type, though the output image is a lower precision 8 bit per channel RGBA image. Prior to that, there was no support for this image.

Pillow (and PIL before it) have had internal support for tiff_lzw compression, but apparently it's either buggy or cannot handle this particular image mode.

Forcing the TiffImagePlugin to use the libtiff decoder allows Pillow to properly decode this image.

@wiredfool wiredfool changed the title TIFF that could not be loaded in 4.2.1 gets silently corrupted in 4.3.0 TIFF_LZW decompression is incorrect for 16bpc image Nov 11, 2017
@wiredfool wiredfool added Bug Any unexpected behavior, until confirmed feature. TIFF labels Nov 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Any unexpected behavior, until confirmed feature. TIFF
Projects
None yet
Development

No branches or pull requests

2 participants