Last active
June 5, 2019 18:55
-
-
Save scossu/27111a4429b8b7f9df1af789741d7c3d to your computer and use it in GitHub Desktop.
test_vips.py
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
#!/usr/bin/env python3 | |
# Use: test_vips.py image_path iterations [parallel] | |
import sys | |
from multiprocessing import Pool | |
import pyvips as vips | |
from pyvips.enums import Interpretation as chmode, Intent | |
SRGB_PROFILE_FPATH = "sRGB2014.icc" | |
ct = int(sys.argv[2]) | |
def _process(i): | |
print(f"Processing image #{i}") | |
vips.cache_set_max(0) | |
fname = sys.argv[1] | |
with open(fname, "rb") as stream: | |
img = vips.Image.new_from_buffer(stream.read(), "", | |
access="sequential" | |
) | |
channels = img.interpretation | |
img = img.icc_transform( | |
SRGB_PROFILE_FPATH, embedded=True, intent=Intent.PERCEPTUAL | |
) | |
kw = { | |
"compression": "jpeg", | |
"Q": 90, | |
"tile": True, | |
"tile_width": 256, | |
"tile_height": 256, | |
"pyramid": True, # Comment this out for getting rid of the memory issue | |
"bigtiff": True, | |
"profile": SRGB_PROFILE_FPATH, | |
} | |
_ = img.tiffsave_buffer(**kw) | |
if __name__ == "__main__": | |
if len(sys.argv) > 3 and sys.argv[3]: | |
with Pool() as pool: | |
pool.map(_process, range(ct)) | |
pool.close() | |
pool.join() | |
else: | |
for i in range(ct): | |
_process(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment