Only getvalue() once per entry

This commit is contained in:
Andrew Murray 2021-06-29 20:18:55 +10:00
parent 8e60ca6106
commit f5558f42f4

View File

@ -319,7 +319,6 @@ def _save(im, fp, filename):
# Size # Size
sizes = [128, 256, 512, 32, 64, 256, 512, 1024] sizes = [128, 256, 512, 32, 64, 256, 512, 1024]
size_str = [b"ic07", b"ic08", b"ic09", b"ic11", b"ic12", b"ic13", b"ic14", b"ic10"] size_str = [b"ic07", b"ic08", b"ic09", b"ic11", b"ic12", b"ic13", b"ic14", b"ic10"]
file_size = 0
entries = [] entries = []
provided_images = {im.width: im for im in im.encoderinfo.get("append_images", [])} provided_images = {im.width: im for im in im.encoderinfo.get("append_images", [])}
temp_sizes = {s: io.BytesIO() for s in set(sizes)} temp_sizes = {s: io.BytesIO() for s in set(sizes)}
@ -328,14 +327,11 @@ def _save(im, fp, filename):
nb.save(temp, "png") nb.save(temp, "png")
for index, s in enumerate(sizes): for index, s in enumerate(sizes):
temp = temp_sizes[s] temp = temp_sizes[s]
file_size += len(temp.getvalue()) stream = temp.getvalue()
entries.append( entries.append(
{ {"type": _to_int(size_str[index]), "size": len(stream), "stream": stream}
"type": _to_int(size_str[index]),
"size": len(temp.getvalue()),
"stream": temp,
}
) )
file_size = sum(entry["size"] for entry in entries)
# Header # Header
fp.write(struct.pack("<i", _to_int(MAGIC))[::-1]) fp.write(struct.pack("<i", _to_int(MAGIC))[::-1])
@ -345,15 +341,15 @@ def _save(im, fp, filename):
toc_size = HEADERSIZE + (len(entries) * HEADERSIZE) toc_size = HEADERSIZE + (len(entries) * HEADERSIZE)
fp.write(struct.pack("<i", _to_int(TOC))[::-1]) fp.write(struct.pack("<i", _to_int(TOC))[::-1])
fp.write(struct.pack("<i", toc_size)[::-1]) fp.write(struct.pack("<i", toc_size)[::-1])
for e in entries: for entry in entries:
fp.write(struct.pack("<i", e["type"])[::-1]) fp.write(struct.pack("<i", entry["type"])[::-1])
fp.write(struct.pack("<i", HEADERSIZE + e["size"])[::-1]) fp.write(struct.pack("<i", HEADERSIZE + entry["size"])[::-1])
# Data # Data
for index, e in enumerate(entries): for entry in entries:
fp.write(struct.pack("<i", e["type"])[::-1]) fp.write(struct.pack("<i", entry["type"])[::-1])
fp.write(struct.pack("<i", HEADERSIZE + e["size"])[::-1]) fp.write(struct.pack("<i", HEADERSIZE + entry["size"])[::-1])
fp.write(e["stream"].getvalue()) fp.write(entry["stream"])
if hasattr(fp, "flush"): if hasattr(fp, "flush"):
fp.flush() fp.flush()