|
6 | 6 | # |
7 | 7 | # history: |
8 | 8 | # 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. |
10 | 10 | # |
11 | 11 | # Copyright (c) 2004 by Bob Ippolito. |
12 | 12 | # Copyright (c) 2004 by Secret Labs. |
@@ -131,6 +131,7 @@ def read_png_or_jpeg2000(fobj, start_length, size): |
131 | 131 |
|
132 | 132 |
|
133 | 133 | class IcnsFile: |
| 134 | + |
134 | 135 | SIZES = { |
135 | 136 | (512, 512, 2): [(b"ic10", read_png_or_jpeg2000)], |
136 | 137 | (512, 512, 1): [(b"ic09", read_png_or_jpeg2000)], |
@@ -305,28 +306,38 @@ def load(self): |
305 | 306 | def _save(im, fp, filename): |
306 | 307 | """ |
307 | 308 | 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. |
309 | 310 | """ |
310 | 311 | if hasattr(fp, "flush"): |
311 | 312 | fp.flush() |
312 | 313 |
|
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 | + } |
317 | 324 | provided_images = {im.width: im for im in im.encoderinfo.get("append_images", [])} |
318 | 325 | 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 | + ) |
321 | 332 |
|
322 | 333 | temp = io.BytesIO() |
323 | 334 | image.save(temp, "png") |
324 | | - size_streams[s] = temp.getvalue() |
| 335 | + size_streams[size] = temp.getvalue() |
325 | 336 |
|
326 | 337 | entries = [] |
327 | | - for index, size in enumerate(sizes): |
| 338 | + for type, size in sizes.items(): |
328 | 339 | 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}) |
330 | 341 |
|
331 | 342 | # Header |
332 | 343 | fp.write(MAGIC) |
|
0 commit comments