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

Handle unexpected ICO image sizes #3836

Merged
merged 1 commit into from May 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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))
22 changes: 17 additions & 5 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,7 +286,15 @@ def load(self):
im.load()
self.im = im.im
self.mode = im.mode
self.size = im.size
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):
# Flag the ImageFile.Parser so that it
Expand Down