Skip to content

Commit

Permalink
Handle unexpected ICO image sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed May 8, 2019
1 parent 0e36b28 commit 47f5c23
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Binary file added Tests/images/hopper_unexpected.ico
Binary file not shown.
7 changes: 7 additions & 0 deletions Tests/test_file_ico.py
Expand Up @@ -83,3 +83,10 @@ def test_only_save_relevant_sizes(self):
self.assertEqual(
im_saved.info['sizes'],
{(16, 16), (24, 24), (32, 32), (48, 48)})

def test_unexpected_size(self):
# This image has been manually hexedited to state that it is 16x32
# while the image within is still 16x16
im = self.assert_warning(UserWarning,
Image.open, "Tests/images/hopper_unexpected.ico")
self.assertEqual(im.size, (16, 16))
19 changes: 15 additions & 4 deletions src/PIL/IcoImagePlugin.py
Expand Up @@ -23,6 +23,7 @@


import struct
import warnings
from io import BytesIO

from . import Image, ImageFile, BmpImagePlugin, PngImagePlugin
Expand Down Expand Up @@ -143,14 +144,17 @@ def sizes(self):
"""
return {(h['width'], h['height']) for h in self.entry}

def getentryindex(self, size, bpp=False):
for (i, h) in enumerate(self.entry):
if size == h['dim'] and (bpp is False or bpp == h['color_depth']):
return i
return 0

def getimage(self, size, bpp=False):
"""
Get an image from the icon
"""
for (i, h) in enumerate(self.entry):
if size == h['dim'] and (bpp is False or bpp == h['color_depth']):
return self.frame(i)
return self.frame(0)
return self.frame(self.getentryindex(size, bpp))

def frame(self, idx):
"""
Expand Down Expand Up @@ -282,6 +286,13 @@ def load(self):
im.load()
self.im = im.im
self.mode = im.mode
if im.size != self.size:
warnings.warn("Image was not the expected size")

index = self.ico.getentryindex(self.size)
sizes = list(self.info['sizes'])
sizes[index] = im.size
self.info['sizes'] = set(sizes)
self.size = im.size

def load_seek(self):
Expand Down

0 comments on commit 47f5c23

Please sign in to comment.