Skip to main content

Images

This page will cover importing and exporting images

This app imports an image, changes its width and height to 200 using the .thumbnail function from the Python Image Library (PIL). One FileInput is used with a variable name of file and the Accept option is set to image/* in order to limit the uploaded files to those with recognized image extensions. The inputs page is shown in the image below:

img alt

Two utility functions are provided:

  • Image_File generates a downloadable file using the <a> HTML tag.
  • Image_Embed generates an embeddable image using the <img> HTML tag .

The Python io library is used to create and edit file objects. The code used is given below:

from PIL import Image
import base64
import io

def Image_File(data,extension, filename):
return "<a href='" + data + "' download='" + filename + "." + extension + "'>Download Image</a>"
def Image_Embed(data):
return "<img src='" + data + "'>"

def main(inputs):
[meta, data] = inputs['file'].split(";base64,")
metadata = meta + ";base64,"

# Decode the file data
file_data = io.BytesIO(base64.b64decode(data))

# Convert the file data into a Pillow's Image
img = Image.open(file_data)

# Manipulate the image
WIDTH = HEIGHT = 200
img.thumbnail((WIDTH, HEIGHT)) # resize

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

imagefile = Image_File(encoded_data,img.format,"MyImage")
image = Image_Embed(encoded_data)
return {
"Image": image, # Display image using img tag
"File": imagefile, # Download using <a> tags
}

Finally, the output page has the following lines:

Displaying Image
{{ outputs.Image}}

Downloading Image
{{ outputs.File}}