Skip to content

Commit 43f5a5f

Browse files
committed
Combined sizes and types into dictionary
1 parent 8736a74 commit 43f5a5f

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/PIL/IcnsImagePlugin.py

+22-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
# history:
88
# 2004-10-09 fl Turned into a PIL plugin; removed 2.3 dependencies.
9-
# 2020-04-04 Allow saving on all operating systems.
9+
# 2020-04-04 Allow saving on all operating systems.
1010
#
1111
# Copyright (c) 2004 by Bob Ippolito.
1212
# Copyright (c) 2004 by Secret Labs.
@@ -131,6 +131,7 @@ def read_png_or_jpeg2000(fobj, start_length, size):
131131

132132

133133
class IcnsFile:
134+
134135
SIZES = {
135136
(512, 512, 2): [(b"ic10", read_png_or_jpeg2000)],
136137
(512, 512, 1): [(b"ic09", read_png_or_jpeg2000)],
@@ -305,28 +306,38 @@ def load(self):
305306
def _save(im, fp, filename):
306307
"""
307308
Saves the image as a series of PNG files,
308-
that are then converted to a .icns file
309+
that are then combined into a .icns file.
309310
"""
310311
if hasattr(fp, "flush"):
311312
fp.flush()
312313

313-
# Size
314-
sizes = [128, 256, 512, 32, 64, 256, 512, 1024]
315-
size_str = [b"ic07", b"ic08", b"ic09", b"ic11", b"ic12", b"ic13", b"ic14", b"ic10"]
316-
314+
sizes = {
315+
b"ic07": 128,
316+
b"ic08": 256,
317+
b"ic09": 512,
318+
b"ic10": 1024,
319+
b"ic11": 32,
320+
b"ic12": 64,
321+
b"ic13": 256,
322+
b"ic14": 512,
323+
}
317324
provided_images = {im.width: im for im in im.encoderinfo.get("append_images", [])}
318325
size_streams = {}
319-
for s in set(sizes):
320-
image = provided_images[s] if s in provided_images else im.resize((s, s))
326+
for size in set(sizes.values()):
327+
image = (
328+
provided_images[size]
329+
if size in provided_images
330+
else im.resize((size, size))
331+
)
321332

322333
temp = io.BytesIO()
323334
image.save(temp, "png")
324-
size_streams[s] = temp.getvalue()
335+
size_streams[size] = temp.getvalue()
325336

326337
entries = []
327-
for index, size in enumerate(sizes):
338+
for type, size in sizes.items():
328339
stream = size_streams[size]
329-
entries.append({"type": size_str[index], "size": len(stream), "stream": stream})
340+
entries.append({"type": type, "size": len(stream), "stream": stream})
330341

331342
# Header
332343
fp.write(MAGIC)

0 commit comments

Comments
 (0)