Error in saving after uploading GIF file in File Upload of Streamlit

Hi, everyone… I’m trying to work on the GIF files and I’m easily able to upload it through file upload of streamlit but what comes next is giving me an error.

Usually, for Images I’m passing it through
im=Image.open(file_upload)
im.save(path_to_save)

and it works fine but when I try and do the same thing with the GIF’s it’s showing an error. I know that’s because the formats and files are different but how can I do that when I’m uploading few things together (GIF, JPG and PNG).

Hi @Alterperiod welcome to the community !

If you only want to save the files you can try doing

import streamlit as st
if file:
    file = st.file_uploader("Upload here")
    open(file.name, "wb").write(file.getbuffer())

If you want to process files based on the extension you can try doing,

import streamlit as st
from pathlib import Path


file = st.file_uploader("Upload here")

if file:
    extension = Path(file.name).suffix
    if extension.upper() == ".GIF":
        # do something special here
        pass
        
    open(file.name, "wb").write(file.getbuffer())

Hope it helps !

1 Like

Thanks for your help…

extension = Path(file.name).suffix
if extension.upper() == ".GIF":

This is what I want and it’s working… :hugs:

1 Like