Retrieve url where image is served / Image ID

Hello dear community,

My streamlit app is running locally streamlit==1.30.0 on python==3.11.7

I am currently building a Streamlit App that leverages different StableDiffusion APIs. For ComfyUIs Controlnet to work I need to serve an image at an URL.

From my understanding streamlits MediaFileManager does just that. β†’ Right click and copy image url will provide me with an url to my image on localhost.

I could not find a way to access neither this image url or the image id to build this url:

Maybe someone is able to help me with that :smile:

β†’ My fallback solution would be to save the generated image to a folder and use Streamlits ability for static file serving Static file serving - Streamlit Docs. But then I would have to worry about cleaning up the files after a session ends.

Thanks for the help!

Hello @haasan,

You could implement your fallback solution in a Streamlit app, along with a simple cleanup mechanism:

import streamlit as st
import os
import shutil
from PIL import Image
import uuid

# Directory where images will be saved
IMAGES_DIR = "served_images"

# Create the directory if it does not exist
if not os.path.exists(IMAGES_DIR):

# Function to save an image and return its URL
def save_image(image_data):
    image_id = str(uuid.uuid4())
    image_path = os.path.join(IMAGES_DIR, f"{image_id}.png")
    return st.experimental_get_server_url() + image_path

# Example usage
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
if uploaded_file:
    image =
    image_url = save_image(image)
    st.write(f"Image URL: {image_url}")

    # Display the image using the URL

# Optional: Cleanup images directory upon session end
# This can be more complex depending on your app's logic and needs
def cleanup_images_dir():
    def cleanup():
        if os.path.exists(IMAGES_DIR):
    return cleanup


Kind Regards,
Sahir Maharaj
Data Scientist | AI Engineer

P.S. Lets connect on LinkedIn!

➀ Website:
➀ Email:
➀ Want me to build your solution? Lets chat about how I can assist!
➀ Join my Medium community of 30k readers! Sharing my knowledge about data science and AI
➀ 100+ FREE Power BI Themes: Download Now

1 Like

You need to use your publicly accesible address. locahost is accesible only from the same machine.