Abrar Shahid
Abrar Shahid

Reputation: 21

Can't Demonstrate uploaded Image in Streamlit with PIL

I was working on displaying uploaded file in Streamlit, but I got an error. Here, the user will upload an image then the image will be displayed .I am using PIL 8.0.1 and Python 3.9 and obviously STREAMLIT.I will be grateful if anyone can help me Here is my code:

import cv2 as cv
import streamlit as st
from PIL import Image,ImageEnhance
import numpy as np
import os
import streamlit.components.v1 as components

@st.cache
def load_image():
    im = Image.open(img)
    return im


def main():
        html_temp = """
        <div style="background-color:#eb4034;padding:3px;border-radius:10px;font-family:'Dosis', sans-serif">
        <h1 style="color:white;text-align:center;">Imagetry</h1>
        
        </div>
        <div class = "lol" style="padding:-5px;font-family:'Dosis', sans-serif">
        <h2 style="color:#292726;text-align:center;">Be alive with Image!</h2>
        </div>
        
        """
        
        components.html(html_temp)
        activities =["Editing"]
        choice = st.sidebar.selectbox("Select Activity",activities)
        if choice == 'Editing':
            
            img = st.file_uploader("Upload Image",type=['jpg','png','jpeg'])
            
            if img is not None:
                file_details = {"Filename":img.name,"FileType":img.type,"FileSize":img.size}
                st.write(file_details)
                image = Image.open(img)
                st.text("Original Image")
                st.image(img,use_column_width=True)
                
                
            
if __name__== '__main__':
        main()

and my Error was :

UnidentifiedImageError: cannot identify image file <streamlit.uploaded_file_manager.UploadedFile object at 0x000001ED946E6680>
Traceback:
File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\streamlit\script_runner.py", line 332, in _run_script
    exec(code, module.__dict__)
File "C:\Users\user\coding\pythonproject\image_editing_web_app\main.py", line 43, in <module>
    main()
File "C:\Users\user\coding\pythonproject\image_editing_web_app\main.py", line 36, in main
    image = Image.open(img)
File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\PIL\Image.py", line 2943

Upvotes: 2

Views: 2039

Answers (1)

Rafael Colombo
Rafael Colombo

Reputation: 435

You're trying to display (st.image) img instead of image.

This is your code:

if img is not None:
    file_details = {"Filename":img.name,"FileType":img.type,"FileSize":img.size}
    st.write(file_details)
    image = Image.open(img)
    st.text("Original Image")
    st.image(img,use_column_width=True)

See that you're opening img in the variable image, so image should be displayed.

 st.image(image, use_column_width = True)

Upvotes: 0

Related Questions