valohai / valohai-utils

Python helper library for Valohai
MIT License
2 stars 2 forks source link

API v2 #28

Closed JuhaKiili closed 3 years ago

JuhaKiili commented 3 years ago

Refactor to the API v2

Example API usage demoing all the different features:

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",
        "https://homepages.cae.wisc.edu/~ece533/images/airplane.png",
    ]
}
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
            )