Skip to content

Commit 93b2185

Browse files
committed
If image is smaller than (16, 16), save original size by default
1 parent 5ca4ebc commit 93b2185

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

Tests/test_file_ico.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ def test_incorrect_size() -> None:
187187
im.size = (1, 1)
188188

189189

190+
def test_save_1x2(tmp_path: Path) -> None:
191+
im = Image.new("1", (1, 2))
192+
outfile = tmp_path / "temp.ico"
193+
im.save(outfile)
194+
195+
with Image.open(outfile) as reloaded:
196+
assert_image_equal(im, reloaded)
197+
198+
190199
def test_save_256x256(tmp_path: Path) -> None:
191200
"""Issue #2264 https://github.com/python-pillow/Pillow/issues/2264"""
192201
# Arrange
@@ -195,9 +204,9 @@ def test_save_256x256(tmp_path: Path) -> None:
195204

196205
# Act
197206
im.save(outfile)
198-
with Image.open(outfile) as im_saved:
207+
with Image.open(outfile) as reloaded:
199208
# Assert
200-
assert im_saved.size == (256, 256)
209+
assert reloaded.size == (256, 256)
201210

202211

203212
def test_only_save_relevant_sizes(tmp_path: Path) -> None:
@@ -211,9 +220,9 @@ def test_only_save_relevant_sizes(tmp_path: Path) -> None:
211220
# Act
212221
im.save(outfile)
213222

214-
with Image.open(outfile) as im_saved:
223+
with Image.open(outfile) as reloaded:
215224
# Assert
216-
assert im_saved.info["sizes"] == {(16, 16), (24, 24), (32, 32), (48, 48)}
225+
assert reloaded.info["sizes"] == {(16, 16), (24, 24), (32, 32), (48, 48)}
217226

218227

219228
def test_save_append_images(tmp_path: Path) -> None:

docs/handbook/image-file-formats.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,8 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options:
452452
**sizes**
453453
A list of sizes included in this ico file; these are a 2-tuple,
454454
``(width, height)``; Default to ``[(16, 16), (24, 24), (32, 32), (48, 48),
455-
(64, 64), (128, 128), (256, 256)]``. Any sizes bigger than the original
456-
size or 256 will be ignored.
455+
(64, 64), (128, 128), (256, 256)]``, or if it is smaller, only the image size.
456+
Any sizes bigger than the original size or 256 will be ignored.
457457

458458
The :py:meth:`~PIL.Image.Image.save` method can take the following keyword arguments:
459459

src/PIL/IcoImagePlugin.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,18 @@
5757
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
5858
fp.write(_MAGIC) # (2+2)
5959
bmp = im.encoderinfo.get("bitmap_format") == "bmp"
60-
sizes = im.encoderinfo.get(
61-
"sizes",
62-
[(16, 16), (24, 24), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)],
63-
)
60+
if "sizes" in im.encoderinfo:
61+
sizes = sorted(set(im.encoderinfo["sizes"]))
62+
else:
63+
sizes = (
64+
[im.size]
65+
if min(im.size) < 16
66+
else [(d, d) for d in (16, 24, 32, 48, 64, 128, 256)]
67+
)
6468
frames = []
6569
provided_ims = [im] + im.encoderinfo.get("append_images", [])
66-
width, height = im.size
67-
for size in sorted(set(sizes)):
68-
if size[0] > width or size[1] > height or size[0] > 256 or size[1] > 256:
70+
for size in sizes:
71+
if size[0] > min(256, im.width) or size[1] > min(256, im.height):
6972
continue
7073

7174
for provided_im in provided_ims:

0 commit comments

Comments
 (0)