MecSimCalc
ExploreSearchCreateDocsCommunityBlogPricing
    Apps

    Starred
    Go to app
    App Docs

Pillow (PIL)

Pillow is a library that offers image processing capabilities
See Pillow documentation for more details


1 - Upload image


2 - Read file into python

Split the data string into metadata and data

[meta, data] = inputs['file'].split(";base64,")

Decode the base64 string into a file type

file_data = io.BytesIO(base64.b64decode(data))

and then convert to Pillow Image

img = Image.open(file_data)


3 - Convert Pillow image back to data string

To view the new image, the image must be converted to a base64 string. Use img.save() to save the image to the io.BytesIO(). Then encode the saved image in buffer into a base64 string using b64encode

buffer = io.BytesIO()
img.save(buffer, format=img.format)
encoded_data = metadata + base64.b64encode(buffer.getvalue()).decode()


4 - Return the image in img tag

Return to outputs using the HTML img tag, where src is the source of the image (ie. the encoded image data)

return {
    "img": "",
}


5 - Display the outputs

Get the img variable from the outputs keyword

{{ outputs.img }}
Similar apps:
Tutorial
Tutorial

Copyright © MecSimCalc 2024
Terms | Privacy