Skip to content

Commit 10669b1

Browse files
authored
Merge pull request #1 from radarhere/icns
Save in ICNS format to support all operating systems
2 parents cc1e78c + ea98bb6 commit 10669b1

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

Tests/test_file_icns.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import io
2-
import sys
32

43
import pytest
54
from PIL import IcnsImagePlugin, Image
@@ -25,7 +24,6 @@ def test_sanity():
2524
assert im.format == "ICNS"
2625

2726

28-
@pytest.mark.skipif(sys.platform != "darwin", reason="Requires macOS")
2927
def test_save(tmp_path):
3028
temp_file = str(tmp_path / "temp.icns")
3129

@@ -38,7 +36,6 @@ def test_save(tmp_path):
3836
assert reread.format == "ICNS"
3937

4038

41-
@pytest.mark.skipif(sys.platform != "darwin", reason="Requires macOS")
4239
def test_save_append_images(tmp_path):
4340
temp_file = str(tmp_path / "temp.icns")
4441
provided_im = Image.new("RGBA", (32, 32), (255, 0, 0, 128))

src/PIL/IcnsImagePlugin.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,13 @@ def load(self):
301301

302302

303303
def to_int(s):
304-
b = s.encode('ascii')
304+
b = s.encode("ascii")
305305
return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]
306306

307307

308308
MAGIC = to_int("icns")
309309
HEADER_SIZE = 8
310-
TOC = 'TOC '
310+
TOC = "TOC "
311311

312312

313313
def _save(im, fp, filename):
@@ -322,37 +322,36 @@ def _save(im, fp, filename):
322322

323323
# size
324324
sizes = [128, 256, 512, 32, 64, 256, 512, 1024]
325-
size_str = ['ic07', 'ic08', 'ic09', 'ic11', 'ic12', 'ic13', 'ic14', 'ic10']
325+
size_str = ["ic07", "ic08", "ic09", "ic11", "ic12", "ic13", "ic14", "ic10"]
326326
file_size = 0
327327
entries = []
328+
provided_images = {im.width: im for im in im.encoderinfo.get("append_images", [])}
328329
for index, s in enumerate(sizes):
329330
temp = io.BytesIO()
330-
nb = im.resize((s, s))
331-
nb.save(temp, 'png')
331+
nb = provided_images[s] if s in provided_images else im.resize((s, s))
332+
nb.save(temp, "png")
332333
file_size += len(temp.getvalue())
333-
entries.append({
334-
'type': size_str[index],
335-
'size': len(temp.getvalue()),
336-
'stream': temp
337-
})
334+
entries.append(
335+
{"type": size_str[index], "size": len(temp.getvalue()), "stream": temp}
336+
)
338337

339338
# Header
340-
fp.write(struct.pack('i', MAGIC)[::-1])
341-
fp.write(struct.pack('i', file_size)[::-1])
339+
fp.write(struct.pack("i", MAGIC)[::-1])
340+
fp.write(struct.pack("i", file_size)[::-1])
342341

343342
# TOC
344343
toc_size = HEADER_SIZE + (len(entries) * HEADER_SIZE)
345-
fp.write(struct.pack('i', to_int(TOC))[::-1])
346-
fp.write(struct.pack('i', toc_size)[::-1])
344+
fp.write(struct.pack("i", to_int(TOC))[::-1])
345+
fp.write(struct.pack("i", toc_size)[::-1])
347346
for e in entries:
348-
fp.write(struct.pack('i', to_int(e.get('type')))[::-1])
349-
fp.write(struct.pack('i', HEADER_SIZE + e.get('size'))[::-1])
347+
fp.write(struct.pack("i", to_int(e.get("type")))[::-1])
348+
fp.write(struct.pack("i", HEADER_SIZE + e.get("size"))[::-1])
350349

351350
# Data
352351
for index, e in enumerate(entries):
353-
fp.write(struct.pack('i', to_int(e.get('type')))[::-1])
354-
fp.write(struct.pack('i', HEADER_SIZE + e.get('size'))[::-1])
355-
fp.write(e.get('stream').getvalue())
352+
fp.write(struct.pack("i", to_int(e.get("type")))[::-1])
353+
fp.write(struct.pack("i", HEADER_SIZE + e.get("size"))[::-1])
354+
fp.write(e.get("stream").getvalue())
356355

357356
fp.flush()
358357

0 commit comments

Comments
 (0)