From 4500acb27471f37b5cf9c8de910c28334db6dce7 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 4 Apr 2020 15:02:02 +1100 Subject: [PATCH 1/3] Lint fixes --- src/PIL/IcnsImagePlugin.py | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/PIL/IcnsImagePlugin.py b/src/PIL/IcnsImagePlugin.py index ce317f12c20..8d02e44e482 100644 --- a/src/PIL/IcnsImagePlugin.py +++ b/src/PIL/IcnsImagePlugin.py @@ -19,11 +19,8 @@ import io import os -import shutil import struct -import subprocess import sys -import tempfile from PIL import Image, ImageFile, PngImagePlugin from PIL._binary import i8 @@ -304,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): @@ -325,37 +322,35 @@ 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 = [] for index, s in enumerate(sizes): temp = io.BytesIO() nb = im.resize((s, s)) - nb.save(temp, 'png') + 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() From 5ef382f61279185dec365cd2122ef2768c8cd237 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 4 Apr 2020 15:02:15 +1100 Subject: [PATCH 2/3] Test all operating systems --- Tests/test_file_icns.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/Tests/test_file_icns.py b/Tests/test_file_icns.py index aeb146f7ec2..9278888fcac 100644 --- a/Tests/test_file_icns.py +++ b/Tests/test_file_icns.py @@ -1,5 +1,4 @@ import io -import sys import pytest from PIL import IcnsImagePlugin, Image @@ -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") @@ -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)) From ea98bb61638f3c65ecd8236798ff85a83b211476 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 4 Apr 2020 14:54:49 +1100 Subject: [PATCH 3/3] Restored append_images --- src/PIL/IcnsImagePlugin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PIL/IcnsImagePlugin.py b/src/PIL/IcnsImagePlugin.py index 8d02e44e482..8a576315cb2 100644 --- a/src/PIL/IcnsImagePlugin.py +++ b/src/PIL/IcnsImagePlugin.py @@ -325,9 +325,10 @@ def _save(im, fp, filename): 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 = provided_images[s] if s in provided_images else im.resize((s, s)) nb.save(temp, "png") file_size += len(temp.getvalue()) entries.append(