How to display an image using streamlit

In this recipe, we will learn how to display an image using Streamlit. We will also take a look at a simple Streamlit application displaying an image.

Recipe Objective: How to display an image in streamlit?

You can display an image in Streamlit using "st.image".

 Syntax: st.image(image, caption=None, width=None, use_column_width=None, clamp=False, channels='RGB', output_format='auto')
 Parameter:
   image -> The image to be displayed. Could be a Monochrome image of shape (w,h) or (w,h,1) or a color image of shape (w,h,3) or an RGBA image of shape (w,h,4) or an image URL OR a path of a local image file OR an SVG XML string OR a list of one of the above in order to display multiple images.

   caption -> Caption for the image If there are many images to be displayed, the caption should be a list of captions (one for each image).

   width -> Width of the image. If none, the picture width is used, but not more than the column width. Because SVG images have no default image width, this property should be specified.

   use_column_width -> Set the image's width to its natural size if 'auto' is selected, but do not exceed the column's width. It sets the image's width to the column width if 'always' or True and sets the image's width to its normal size if 'never' or False. If use column width is set, it takes precedence over the width parameter.

   clamp -> Clamp image pixel values to a valid range ([0-255] per channel). This option is only relevant for byte array images; it is ignored for image URLs. An error will be raised if this is not set and an image has an out-of-range value.

   channels -> If image is an nd.array, this parameter specifies the format used to represent color information.

   output_format -> This parameter sets the picture data transfer format to be used. For lossy compression, photos should be saved in JPEG format, while diagrams should be saved in PNG format for lossless compression. The default value is 'auto,' which determines the compression type depending on the image argument's type and format.

Code:

#importing streamlit library
import streamlit as st
from PIL import Image

#opening the image
image = Image.open('imagefile')

#displaying the image on streamlit app
st.image(image, caption='Enter any caption here')

To run the app, either create an appname.py file with the above code using any text editor, or if you are using a jupyter notebook, you need to download your .ipynb notebook as a Python (.py) file and run the same using the "streamlit run appname.py" command. Once you run the command, the app will automatically open in your default browser.

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.