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

"cannot identify image file" in 3.0.0 on a TIFF file that worked with 2.9.0 #1466

Closed
pawl opened this issue Oct 3, 2015 · 7 comments
Closed

Comments

@pawl
Copy link

pawl commented Oct 3, 2015

To test, first download the files in this folder: https://github.com/flask-admin/flask-admin/tree/master/flask_admin/tests/data

Then run this code in the same folder as those files:

from PIL import Image

file_list = [
    'copyleft.gif',
    'copyleft.jpeg',
    'copyleft.jpg',
    'copyleft.png',
    'copyleft.tiff',
]

for filename in file_list:
    with open(filename, 'rb') as fp:
        image = Image.open(fp)
        print("successfully opened:", filename)

It will open all 5 files in 2.9.0, but in 3.0.0 it will fail on the TIFF file. It's failing on OS X Yosemite and Ubuntu 12.04 LTS Server Edition 64 bit with both Python 2 and 3.

The error I'm getting is: IOError: cannot identify image file <open file 'copyleft.tiff', mode 'rb' at 0x10a15f8a0>

@wiredfool
Copy link
Member

Well, It's the Tiff IFD change.

Looks like in the reorg of the format specifications, this one got dropped:

...
- compression: packbits
- photometric_interpretation: 2
- planar_configuration: 1
- fill_order: 1
- size: (220, 220)
format key: ('MM', 2, (1, 1, 1), 1, (8, 8, 8), ())
- unsupported format

@wiredfool
Copy link
Member

The tiff spec defines SAMPLEFORMAT as N shorts, where N is the number of bands in the image.

For the old code, the sample format was set by getscalar(SAMPLEFORMAT, (1,)), where getformat was defined as:

    def getscalar(self, tag, default=None):
        try:
            value = self[tag]
            if len(value) != 1:
                if tag == SAMPLEFORMAT:
                    # work around broken (?) matrox library
                    # (from Ted Wright, via Bob Klimek)
                    raise KeyError  # use default
                raise ValueError("not a scalar")
            return value[0]
        except KeyError:
            if default is None:
                raise
            return default

Which I think simply means that SAMPLEFORMAT isn't a scalar, and would always return the default (1,) value unless we happened to have a single band image, in which case, we would actually deal with it correctly. Helpfully enough, we don't actually support anything but unsigned ints in multiband images, so practically we wound up doing the right thing for the wrong reasons.

The current code just uses the SAMPLEFORMAT tag directly, which is fine for single band images but since we don't have any multi-item tuples defined, it's wrong for any color tiff that sets the SAMPLEFORMAT tag.

--edit--
It looks like we've hit this before, since there are OSX grab lines that have the correct SAMPLEFORMAT tuple.

wiredfool added a commit to wiredfool/Pillow that referenced this issue Oct 3, 2015
@wiredfool
Copy link
Member

Try this branch: https://github.com/wiredfool/Pillow/tree/sampleformat

Is the copyleft.tiff diestibutable under an MIT like license as a test image?

(I'm somewhat worried about the war that that question could ignite...)

wiredfool added a commit to wiredfool/Pillow that referenced this issue Oct 3, 2015
@pawl
Copy link
Author

pawl commented Oct 3, 2015

@wiredfool Works great! My example above works now. The image uploaded successfully and it fixed our tests in flask-admin too: https://i.imgur.com/l1naZYv.png

Thank you so much for your hard work on Pillow.

@wiredfool
Copy link
Member

Cool, glad it worked.

@niksumeiko
Copy link

I am wondering whether and when Pillow 3.0.0 is going to have this issue fixed?

Since the same issue happened to me when using Image.open('image.tiff') with latest Pillow, originally installed with pip install Pillow.
However, all works, when installed from a repo @wiredfool pointed out:

pip install git+https://github.com/wiredfool/Pillow

@radarhere
Copy link
Member

The fix is included in the latest release of Pillow, 3.1.0.

At the moment, there are no planned subsequent releases of Pillow 3.0.

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

No branches or pull requests

4 participants