StreamlitAPIException: copy() is not a valid Streamlit command

Hi all, I’m new here and I’m not much familiar with streamlit. i don’t know which command is used for copy the input data in another variable.
image = st.image(uploaded_file, width=700)
newimg = image.copy()

I try to copy the input in another variable with these lines of code but i got the error
StreamlitAPIException: copy() is not a valid Streamlit command.
How can that be done?

Hi @Nadia_Liaquat, welcome to the Streamlit community! :tada: :partying_face:

There are a couple of ways to go about it. You could do newimg = uploaded_file to assign the input data to another variable newimg.

If you’re used to working with PIL to process images, you could convert uploaded_file to a PIL image, and use PIL.Image.copy() to copy the image to another variable.

Here’s an example with both methods:

import streamlit as st
from PIL import Image

# upload image
uploaded_file = st.file_uploader("Upload image", type=['png', 'jpg'])

# check if image is uploaded
if uploaded_file:
    # display uploaded image
    image = st.image(uploaded_file, caption="original image", width=300)

    # copy uploaded_file and display copy
    newimg = uploaded_file
    st.image(newimg, caption="newimg", width=100)

    # open uploaded_file as PIL image
    pil_img = Image.open(uploaded_file)
    image = st.image(pil_img, caption="PIL image")

    # use PIL.Image.copy() and display copied image
    newimg = pil_img.copy() # "copy the input data in another variable"
    st.image(newimg, caption="Copy of PIL image", width=200)

Hope this helps! Let us know if you have follow-up questions :slight_smile:

Happy Streamlit-ing! :balloon:
Snehan

Thank you @snehankekre. it works

1 Like