Whoops…Nothing found

Try other keywords in your search

Get Images into a Flow

 1 Minute



When is this applicable?

This article explains how to display images or figures in a Virtualitics AI Platform Flow using the PIL library. 

The Virtualitics AI Platform has internal support for generating plots, infographics, and dashboards. In some Flows, rather than using the Virtualitics AI Platform built-in plots, you may want to generate your own plots using Python libraries such as seaborn or matplotlib.

You may also need to display an image in a Flow, such as one from an external URL. 



For both external URL images and seaborn / matplotlib plots, you will need to add the following import statement into your flow: 

from PIL import Image as PILImage
from io import BytesIO
import requests # external images only!


External URL Images  

In order to display an Image from an external URL in your Virtualitics AI Platform Flow, you will also need to add import requests to the top of the file, as shown above.

At this point, in the part of the Step where you would like to display the image, add the following lines:

url = 'https://virtualitics.com/wp-content/uploads/2022/03/genericblogpost.png'
response = requests.get(url)
img = PILImage.open(BytesIO(response.content))
my_image_element = Image(content=img, title="Virtualitics Header", 
                        description="External URL Image.")



At the top of your flow’s Python file (e.g. projects/my-flow.py), add the following lines:

from PIL import Image as PILImage
def fig2img(fig):
   """Convert a Matplotlib figure to a PIL Image and return it"""
   buf = io.BytesIO()
   img = PILImage.open(buf)
   return img


Inside of the Step where you would like to display the plot, add the following lines.

# Most likely you aren't using clustermap but it should work the same for other plot types
g = sns.clustermap(data, method='ward', figsize=(12, 8))
plot_as_bytes = fig2img(g.fig)
my_image_element = Image(content=plot_as_bytes, title="My Title Here", 
                        description="Describe the stuff.")


What to Expect (Validation) 

The image will be displayed in your Flow.


Additional Details 

If attempting to display images from URLs, make sure the image is accessible via the requests library when using the appropriate parameters.

Was this article helpful?