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

Thumbnailing a single gif: 'IOError: codec configuration error when reading image file' #2811

Closed
Lavr18 opened this issue Oct 25, 2017 · 7 comments

Comments

@Lavr18
Copy link

Lavr18 commented Oct 25, 2017

What did you do?

I was trying to thumbnail a gif image and save it

from PIL import Image

im = Image.open("Pictures/image.gif")
im.thumbnail((240,160))
im.save("Thumbnails/thumbnail.gif")

What did you expect to happen?

I expeted to get a gif thumbnail in the "/Thumbnails/" directory.
This same code worked for jpg, png, bmp formats.

What actually happened?

This is the log:

Traceback (most recent call last):
  File "testpy.py", line 4, in <module>
    im.thumbnail((240,180))
  File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1843, in thumbnail
    im = self.resize(size, resample)
  File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1541, in resize
    self.load()
  File "/usr/lib/python2.7/dist-packages/PIL/ImageFile.py", line 244, in load
    raise_ioerror(err_code)
  File "/usr/lib/python2.7/dist-packages/PIL/ImageFile.py", line 59, in raise_ioerror
    raise IOError(message + " when reading image file")
IOError: codec configuration error when reading image file

The issue doesn't occur though with ImageFile.LOAD_TRUNCATED_IMAGES = True, but in this case the thumbnail is completely black with the size of 213x160 instead of 240x160.

What versions of Pillow and Python are you using?

I am using Pillow 4.3.0, Python 2.7.13

@hugovk
Copy link
Member

hugovk commented Oct 25, 2017

Does it work with other gif files? Please can you share the problematic gif?

@radarhere radarhere changed the title Thumbnaling a single gif: 'IOError: codec configuration error when reading image file' Thumbnailing a single gif: 'IOError: codec configuration error when reading image file' Oct 25, 2017
@Lavr18
Copy link
Author

Lavr18 commented Oct 25, 2017

This works with gif images saved from the internet. For example, with this:
image3

The error occurs when working with gif images made by my raspistill camera. For example, with this:
image4

Edit:
This error happens when I specifically select encoding mode as gif in raspistill camera.
If I don't select encoding mode at all, it works well.
But if not selecting encoding, then all the images are treated as jpg, which is not what I want.

@hugovk
Copy link
Member

hugovk commented Oct 25, 2017

Slightly smaller reproduction:

from PIL import Image
im = Image.open("2811.gif")
im.load()  # IOError

https://github.com/python-pillow/Pillow/blob/master/PIL/ImageFile.py#L236 returns -1, -8 in n, err_code = decoder.decode(b).

Adding this beforehand allows it to be loaded, but it's a blank, black image:

from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

@wiredfool
Copy link
Member

wiredfool commented Oct 25, 2017

It's kind of a funky gif, it's setting a bit depth of 11:

>>> im.tile
[('gif', (0, 0, 100, 100), 792, (11, False))]

That gets set in GifImagePlugin:

                # image data
                bits = i8(self.fp.read(1))
                self.__offset = self.fp.tell()
                self.tile = [("gif",
                             (x0, y0, x1, y1),
                             self.__offset,
                             (bits, interlace))]

Which is then failing in the GIF decoder:

	/* Initialise state */
	if (context->bits < 0 || context->bits > 8) {
	    state->errcode = IMAGING_CODEC_CONFIG;
	    return -1;
	}

Looking at the GIF, it does have a setting of 0B for the number of bits, which is not correct, or at least, not allowed by the decoder.

@wiredfool
Copy link
Member

According to the spec: https://www.w3.org/Graphics/GIF/spec-gif89a.txt (appendix f) it appears that the legal range is 1-12 bits. There doesn't appear to be anything in the decoder that's relying on that bit length to be < 8, so it appears to be a safe change.

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

@Lavr18 Can we use that image in the test suite?

@Lavr18
Copy link
Author

Lavr18 commented Oct 25, 2017

@wiredfool Yes, sure

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

3 participants