Skip to content

Instantly share code, notes, and snippets.

@JuhaKiili
Last active May 27, 2021 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JuhaKiili/c48ca75ae774c69ce5dda52071fe5d39 to your computer and use it in GitHub Desktop.
Save JuhaKiili/c48ca75ae774c69ce5dda52071fe5d39 to your computer and use it in GitHub Desktop.
Example of image resize step supporting archived inputs and outputs using valohai-utils
import os
from PIL import Image
import valohai
parameters = {
"width": 640,
"height": 480,
}
inputs = {
"images": [
"https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg",
]
}
valohai.prepare(step="resize", default_parameters=parameters, default_inputs=inputs)
def resize_image(in_path, out_path, width, height, logger):
image = Image.open(in_path)
logger.log("from_width", image.size[0])
logger.log("from_height", image.size[1])
logger.log("to_width", width)
logger.log("to_height", height)
new_image = image.resize((width, height))
new_image.save(out_path)
if __name__ == '__main__':
for image_path in valohai.inputs('images').paths():
with valohai.metadata.logger() as logger:
filename = os.path.basename(image_path)
resize_image(
in_path=image_path,
out_path=valohai.outputs('images').path(filename),
width=valohai.parameters('width').value,
height=valohai.parameters('height').value,
logger=logger
)
valohai.outputs('images').compress("*", "images.zip", remove_originals=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment