Skip to content

Commit

Permalink
Merge pull request #1 from radarhere/icns
Browse files Browse the repository at this point in the history
Save in ICNS format to support all operating systems
  • Loading branch information
newpanjing committed Apr 7, 2020
2 parents cc1e78c + ea98bb6 commit 10669b1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
3 changes: 0 additions & 3 deletions Tests/test_file_icns.py
@@ -1,5 +1,4 @@
import io
import sys

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


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

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


@pytest.mark.skipif(sys.platform != "darwin", reason="Requires macOS")
def test_save_append_images(tmp_path):
temp_file = str(tmp_path / "temp.icns")
provided_im = Image.new("RGBA", (32, 32), (255, 0, 0, 128))
Expand Down
37 changes: 18 additions & 19 deletions src/PIL/IcnsImagePlugin.py
Expand Up @@ -301,13 +301,13 @@ def load(self):


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


MAGIC = to_int("icns")
HEADER_SIZE = 8
TOC = 'TOC '
TOC = "TOC "


def _save(im, fp, filename):
Expand All @@ -322,37 +322,36 @@ def _save(im, fp, filename):

# size
sizes = [128, 256, 512, 32, 64, 256, 512, 1024]
size_str = ['ic07', 'ic08', 'ic09', 'ic11', 'ic12', 'ic13', 'ic14', 'ic10']
size_str = ["ic07", "ic08", "ic09", "ic11", "ic12", "ic13", "ic14", "ic10"]
file_size = 0
entries = []
provided_images = {im.width: im for im in im.encoderinfo.get("append_images", [])}
for index, s in enumerate(sizes):
temp = io.BytesIO()
nb = im.resize((s, s))
nb.save(temp, 'png')
nb = provided_images[s] if s in provided_images else im.resize((s, s))
nb.save(temp, "png")
file_size += len(temp.getvalue())
entries.append({
'type': size_str[index],
'size': len(temp.getvalue()),
'stream': temp
})
entries.append(
{"type": size_str[index], "size": len(temp.getvalue()), "stream": temp}
)

# Header
fp.write(struct.pack('i', MAGIC)[::-1])
fp.write(struct.pack('i', file_size)[::-1])
fp.write(struct.pack("i", MAGIC)[::-1])
fp.write(struct.pack("i", file_size)[::-1])

# TOC
toc_size = HEADER_SIZE + (len(entries) * HEADER_SIZE)
fp.write(struct.pack('i', to_int(TOC))[::-1])
fp.write(struct.pack('i', toc_size)[::-1])
fp.write(struct.pack("i", to_int(TOC))[::-1])
fp.write(struct.pack("i", toc_size)[::-1])
for e in entries:
fp.write(struct.pack('i', to_int(e.get('type')))[::-1])
fp.write(struct.pack('i', HEADER_SIZE + e.get('size'))[::-1])
fp.write(struct.pack("i", to_int(e.get("type")))[::-1])
fp.write(struct.pack("i", HEADER_SIZE + e.get("size"))[::-1])

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

fp.flush()

Expand Down

0 comments on commit 10669b1

Please sign in to comment.