st.image() in Streamlit
In Streamlit, the st.image() function is used to display images in your web app. It supports local files, URLs, NumPy arrays, and PIL images, making it flexible for dashboards, ML apps, and data visualization.
Basic Syntax
st.image(image, caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
The function has the following parameters:
- image: Path, URL, PIL image, or NumPy array
- caption: Text displayed below the image
- width: Set custom width (pixels)
- use_column_width: Auto-fit image to container width
- clamp: Fix out-of-range pixel values
- channels: "RGB" or "BGR" (important for OpenCV images)
- output_format: Format conversion (auto, PNG, JPEG)
Example: Display a local image.
Python
import streamlit as st
st.title("Local Image Example")
# Display image from local folder
st.image("cat.jpg", caption="My Cat", width=300) - Useful for logos, UI icons, and static images.
Example: Display an image from a URL.
Python
import streamlit as st
st.image(
"https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d",
caption="Image from Internet"
) - Good for dynamic content and online assets.