Thursday, April 14, 2022

[SOLVED] Images not loading when deploying streamlit app on GCP

Issue

I created a streamlit app for data science. The user uploads an image and gets an output with an image displayed, but after deploying the app I'm getting zero rather than an image.

class="lang-py prettyprint-override">import streamlit as st

@st.cache(allow_output_mutation=True)
def load_image(out):
    if out=='yes':
        im = PIL.Image.open("images/yes.jpg")
        return im

dis = load_image(output)
st.image(dis, channels="RGB)

but after deploying I get:

enter image description here


Solution

Solved by transforming the image (OpenCV, pillow ...) into bytes

import base64
import cv2
import streamlit as st
retval, buffer = cv2.imencode('.jpg', img )
binf = base64.b64encode(buffer).decode()
st.image("data:image/png;base64,%s"%binf, channels="BGR", use_column_width=True)


Answered By - mech iyad
Answer Checked By - Willingham (WPSolving Volunteer)