From 9e9e136f542aeaf5afb5a30fa08a1a70733e74d3 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 7 Apr 2020 21:42:23 +1000 Subject: [PATCH] Do not save two temporary files for the same size --- src/PIL/IcnsImagePlugin.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/PIL/IcnsImagePlugin.py b/src/PIL/IcnsImagePlugin.py index 98603e2f9..cf9b5dcf5 100644 --- a/src/PIL/IcnsImagePlugin.py +++ b/src/PIL/IcnsImagePlugin.py @@ -325,10 +325,12 @@ def _save(im, fp, filename): 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() + temp_sizes = {s: io.BytesIO() for s in set(sizes)} + for s, temp in temp_sizes.items(): nb = provided_images[s] if s in provided_images else im.resize((s, s)) nb.save(temp, "png") + for index, s in enumerate(sizes): + temp = temp_sizes[s] file_size += len(temp.getvalue()) entries.append( {"type": size_str[index], "size": len(temp.getvalue()), "stream": temp} @@ -343,14 +345,14 @@ def _save(im, fp, filename): fp.write(struct.pack("