-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
139 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import io | ||
import os | ||
import sys | ||
import timeit | ||
|
||
from PIL import Image | ||
|
||
# PATH = "Tests/images/hopper.png" | ||
# PATH = "Tests/images/effect_spread.png" | ||
PATH = sys.argv[1] | ||
REPEAT = 1000 | ||
|
||
print(f"{Image.__version__ = }") | ||
print(f"{Image.core.zlib_version = }") | ||
print(f"Testing {PATH = } with {REPEAT} repetitions") | ||
|
||
|
||
with open(PATH, "rb") as f: | ||
data_in = f.read() | ||
|
||
reader = io.BytesIO(data_in) | ||
writer = io.BytesIO() | ||
|
||
img = Image.open(reader) | ||
img.load() | ||
|
||
|
||
def read_png(): | ||
reader.seek(0, os.SEEK_SET) | ||
return Image.open(reader) | ||
|
||
|
||
def write_png(compress_level): | ||
writer.seek(0, os.SEEK_SET) | ||
writer.truncate(0) | ||
img.save(writer, "PNG", compress_level=compress_level) | ||
|
||
|
||
t = timeit.timeit(read_png, number=REPEAT * 10) | ||
print(f"read PNG: time = {t:f} (sec)") | ||
|
||
|
||
results = dict() | ||
for compress_level in range(0, 10): | ||
t = timeit.timeit(lambda: write_png(compress_level), number=REPEAT) | ||
print( | ||
f"write PNG: time = {t:f} (sec); " | ||
f"size = {len(writer.getvalue())} bytes; " | ||
f"{compress_level = }" | ||
) | ||
results[compress_level] = (len(writer.getvalue()), t) | ||
|
||
header = f"{Image.core.zlib_version}/{os.path.basename(PATH)}" | ||
|
||
print() | ||
print(header) | ||
for size, t in results.values(): | ||
print(t) | ||
|
||
print() | ||
print(header) | ||
for size, t in results.values(): | ||
print(size) | ||
|
||
# Image.open(io.BytesIO(writer.getvalue())).show() | ||
|
||
# Image.__version__ = '11.1.0.dev0' | ||
# Image.core.zlib_version = '1.3.1' | ||
# Testing PATH = 'Tests/images/hopper.png' with 1000 repetitions | ||
# read PNG: time = 0.121070 (sec) | ||
# write PNG: time = 0.277860 (sec); size = 49353 bytes; compress_level = 0 | ||
# write PNG: time = 1.378973 (sec); size = 32403 bytes; compress_level = 1 | ||
# write PNG: time = 1.463843 (sec); size = 32156 bytes; compress_level = 2 | ||
# write PNG: time = 1.946944 (sec); size = 31792 bytes; compress_level = 3 | ||
# write PNG: time = 2.628619 (sec); size = 30715 bytes; compress_level = 4 | ||
# write PNG: time = 2.989835 (sec); size = 30529 bytes; compress_level = 5 | ||
# write PNG: time = 3.582502 (sec); size = 30343 bytes; compress_level = 6 | ||
# write PNG: time = 4.346017 (sec); size = 30277 bytes; compress_level = 7 | ||
# write PNG: time = 6.963706 (sec); size = 30183 bytes; compress_level = 8 | ||
# write PNG: time = 11.201260 (sec); size = 30166 bytes; compress_level = 9 | ||
|
||
# Image.__version__ = '11.1.0.dev0' | ||
# Image.core.zlib_version = '1.3.1.zlib-ng' | ||
# Testing PATH = 'Tests/images/hopper.png' with 1000 repetitions | ||
# read PNG: time = 0.121355 (sec) | ||
# write PNG: time = 0.243460 (sec); size = 49353 bytes; compress_level = 0 | ||
# write PNG: time = 0.484412 (sec); size = 40354 bytes; compress_level = 1 | ||
# write PNG: time = 0.582722 (sec); size = 31458 bytes; compress_level = 2 | ||
# write PNG: time = 0.647406 (sec); size = 31096 bytes; compress_level = 3 | ||
# write PNG: time = 0.849081 (sec); size = 30836 bytes; compress_level = 4 | ||
# write PNG: time = 0.699703 (sec); size = 30771 bytes; compress_level = 5 | ||
# write PNG: time = 0.916293 (sec); size = 30649 bytes; compress_level = 6 | ||
# write PNG: time = 1.426837 (sec); size = 30217 bytes; compress_level = 7 | ||
# write PNG: time = 1.855467 (sec); size = 30175 bytes; compress_level = 8 | ||
# write PNG: time = 2.772019 (sec); size = 30166 bytes; compress_level = 9 | ||
|
||
# Image.__version__ = '11.1.0.dev0' | ||
# Image.core.zlib_version = '1.3.1' | ||
# Testing PATH = 'Tests/images/effect_spread.png' with 1000 repetitions | ||
# read PNG: time = 0.064432 (sec) | ||
# write PNG: time = 0.271332 (sec); size = 49353 bytes; compress_level = 0 | ||
# write PNG: time = 1.590392 (sec); size = 42199 bytes; compress_level = 1 | ||
# write PNG: time = 1.749554 (sec); size = 42096 bytes; compress_level = 2 | ||
# write PNG: time = 2.188817 (sec); size = 41977 bytes; compress_level = 3 | ||
# write PNG: time = 2.496155 (sec); size = 41011 bytes; compress_level = 4 | ||
# write PNG: time = 2.278722 (sec); size = 40969 bytes; compress_level = 5 | ||
# write PNG: time = 2.514241 (sec); size = 40903 bytes; compress_level = 6 | ||
# write PNG: time = 2.503093 (sec); size = 40896 bytes; compress_level = 7 | ||
# write PNG: time = 2.616062 (sec); size = 40889 bytes; compress_level = 8 | ||
# write PNG: time = 2.614784 (sec); size = 40889 bytes; compress_level = 9 | ||
|
||
# Image.__version__ = '11.1.0.dev0' | ||
# Image.core.zlib_version = '1.3.1.zlib-ng' | ||
# Testing PATH = 'Tests/images/effect_spread.png' with 1000 repetitions | ||
# read PNG: time = 0.062720 (sec) | ||
# write PNG: time = 0.180120 (sec); size = 49353 bytes; compress_level = 0 | ||
# write PNG: time = 0.400339 (sec); size = 48241 bytes; compress_level = 1 | ||
# write PNG: time = 0.682549 (sec); size = 41287 bytes; compress_level = 2 | ||
# write PNG: time = 0.724561 (sec); size = 41168 bytes; compress_level = 3 | ||
# write PNG: time = 0.743060 (sec); size = 41134 bytes; compress_level = 4 | ||
# write PNG: time = 0.756581 (sec); size = 41120 bytes; compress_level = 5 | ||
# write PNG: time = 0.818945 (sec); size = 41118 bytes; compress_level = 6 | ||
# write PNG: time = 0.872731 (sec); size = 40895 bytes; compress_level = 7 | ||
# write PNG: time = 0.824981 (sec); size = 40889 bytes; compress_level = 8 | ||
# write PNG: time = 1.366639 (sec); size = 40889 bytes; compress_level = 9 |