Skip to content

Commit

Permalink
Added support for reading all Windows 1 formats
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Oct 1, 2023
1 parent 505c848 commit 261a728
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 4 deletions.
Binary file added Tests/images/ico1both.ico
Binary file not shown.
Binary file added Tests/images/ico1device_dependent.ico
Binary file not shown.
File renamed without changes.
14 changes: 12 additions & 2 deletions Tests/test_file_ico.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,20 @@ def test_draw_reloaded(tmp_path):
assert_image_equal_tofile(im, "Tests/images/hopper_draw.ico")


def test_ico1_open():
with Image.open("Tests/images/ico1.ico") as im:
@pytest.mark.parametrize(
"test_image_path",
(
"Tests/images/ico1device_independent.ico",
"Tests/images/ico1device_dependent.ico",
"Tests/images/ico1both.ico",
),
)
def test_ico1_open(test_image_path):
with Image.open(test_image_path) as im:
assert_image_equal_tofile(im, "Tests/images/ico1.png")


def test_ico1_invalid_file():
with open("Tests/images/flower.jpg", "rb") as fp:
with pytest.raises(SyntaxError):
IcoImagePlugin.Ico1ImageFile(fp)
Expand Down
3 changes: 2 additions & 1 deletion docs/handbook/image-file-formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ The :py:meth:`~PIL.Image.Image.save` method can take the following keyword argum
ICO1
^^^^

Pillow also reads and writes device-independent Windows 1.0 icons.
Pillow also reads Windows 1.0 icons in all three formats ("Device-Independent",
"Device-Dependent" and "Both"), and writes them in "Device-Independent" format.

.. versionadded:: 10.1.0

Expand Down
2 changes: 1 addition & 1 deletion src/PIL/IcoImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def load_seek(self):


def _ico1_accept(prefix):
return prefix[:2] == b"\1\0"
return prefix[0] == 1 and prefix[1] in (0, 1, 2)


class Ico1ImageFile(ImageFile.ImageFile):
Expand Down

0 comments on commit 261a728

Please sign in to comment.