load ico file with ImageFile will raise an exception #522

Closed
regod opened this Issue Feb 13, 2014 · 7 comments

Comments

Projects
None yet
3 participants

regod commented Feb 13, 2014

Traceback (most recent call last):
File "t.py", line 12, in
p.feed(data)
File "/.../lib/python2.7/site-packages/PIL/ImageFile.py", line 383, in feed
if flag or len(im.tile) != 1:
TypeError: object of type 'NoneType' has no len()

from PIL import ImageFile
data = open('favicon.ico', 'rb').read()
p = ImageFile.Parser()
p.feed(data)

I found when excute im = im.convert('RGBA') in function frame() of IcoImagePlugin.py file, im.tile will lose and turn to NoneType.

Owner

wiredfool commented Feb 13, 2014

Does it work if you just read the file directly?

from PIL import Image
im = Image.open('favicon.ico')
im = im.convert('RGBA')

The parser class isn't always quite as robust as just simply opening and reading the file.

regod commented Feb 14, 2014

It works, but im.tile is still None. So in feed function when checking condition len(im.tile) != 1, the exception will also raise. I guess it is better to set im.tile's value an empty list instead of None.

Owner

wiredfool commented Feb 14, 2014

Can you post the .ico file so I can take a look?

regod commented Feb 17, 2014

github editor can't support ico file, so i give a link the ico file i use.
http://www.python.org/favicon.ico

Owner

wiredfool commented Feb 17, 2014

Works for me:

(vpy27)erics:~/Pillow/Images$ python
Python 2.7.5+ (default, Sep 19 2013, 13:49:51) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> im = Image.open('python.ico')
>>> im.load()
>>> im.size
(48, 48)
>>> im.convert('RGBA')
<PIL.Image.Image image mode=RGBA size=48x48 at 0x955550C>
>>> 

What Pillow are you running?

[[edit]] I see, you're still running the ImageFile.Parser class. Is there a reason that you're doing that instead of just opening directly?

wiredfool added a commit to wiredfool/Pillow that referenced this issue Feb 17, 2014

regod commented Feb 17, 2014

That happens when I use django which is used ImageFile.Parser class in core/files/images.py.

Owner

wiredfool commented Feb 18, 2014

Ah. Django. Try the commit up there, it should patch it so that ico files work with the parser module.

aclark4life added a commit that referenced this issue Mar 17, 2014

Merge pull request #525 from wiredfool/ico_parser
Make ICO files work with the ImageFile.Parser interface, fixes #522
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment