Skip to content

Commit

Permalink
Do not save two temporary files for the same size
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Apr 7, 2020
1 parent 84b7e26 commit 9e9e136
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/PIL/IcnsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -343,14 +345,14 @@ def _save(im, fp, filename):
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", HEADERSIZE + e.get("size"))[::-1])
fp.write(struct.pack("<i", _to_int(e["type"]))[::-1])
fp.write(struct.pack("<i", HEADERSIZE + e["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", HEADERSIZE + e.get("size"))[::-1])
fp.write(e.get("stream").getvalue())
fp.write(struct.pack("<i", _to_int(e["type"]))[::-1])
fp.write(struct.pack("<i", HEADERSIZE + e["size"])[::-1])
fp.write(e["stream"].getvalue())

fp.flush()

Expand Down

0 comments on commit 9e9e136

Please sign in to comment.