Streamlit

Streamlit 에서 gif 이미지 파일 입력하기

Cong_S 2022. 6. 8. 18:07

Streamlit에서 이미지를 삽입할 때 

from PIL import Image

img = Image.open('data2/image_03.jpg')

    st.image(img)
    st.image(img,use_column_width=True)

위와 같은 코드를 사용해 이미지를 삽입하게 되는데

 

이 때 gif 파일은 정상적으로 입력되지 않는 모습을 보인다. 

 

이때는 다음과 같은 코드를 사용해보자.

 

import streamlit as st
import base64

"""### gif from url"""
st.markdown("![Alt Text](https://media.giphy.com/media/vFKqnCdLPNOKc/giphy.gif)")

"""### gif from local file"""
file_ = open("/home/rzwitch/Desktop/giphy.gif", "rb")
contents = file_.read()
data_url = base64.b64encode(contents).decode("utf-8")
file_.close()

st.markdown(
    f'<img src="data:image/gif;base64,{data_url}" alt="cat gif">',
    unsafe_allow_html=True,
)

위의 코드는 gif 파일을 웹에서 가져올 때 사용하고

아래의 코드는 gif 파일을 로컬에서 가져올 때 사용하면 된다.