Saving images in Github repository

Hello,

I’ve created an app that uses the st.input_camera to take some pictures. When deploying the app using my private github repository I can’t save the images anywhere. I’m using the PIL module to try to save them but the program doesn’t save them anywhere.

I am trying to create a zip file with all the pictures in it so then I can download them as I found in this article: Download multiple files

Can someone help me with this issue please? I’ll be very happy if I manage to solve it :slight_smile:

 finished_main = st.button("Finish taking pictures")
    # Trying to add the zip file
        if finished_main:
            zipObj = ZipFile("sample.zip", "w")
            num_main = num_main-1
            for one_picture in all_pics[num_main]:
                with open(one_picture, 'rb') as f:
                    img_to_zip = f.read()
                img_open = Image.open(io.BytesIO(img_to_zip))
                st.write(img_open)
                zipObj.write(img_open+'.jpg')
                #zipObj.write(img_open)
            zipObj.close()
            ZipfileDotZip = "sample.zip"
            
            with open(ZipfileDotZip, "rb") as f:
                bytes = f.read()
                b64 = base64.b64encode(bytes).decode()
                href = f"<a href=\"data:file/zip;base64,{b64}\" download='{ZipfileDotZip}.zip'>\
                    Click last model weights\</a>"
            st.markdown(href, unsafe_allow_html=True)
    # end of trtying

Can you show some code? You’d have to store your images, files, etc. in memory using io.BytesIO and then add that memory buffer to your zip file on the fly (again, creating the zip file in memory). You then pass that zipfile to your download button.

Hello @Wally , yes I edited my question and added the part of the code I use for it. I already used the io.BytesIO to open the images but is not saving them properly as I get a Traceback message error as:
TypeError: stat: path should be string, bytes, os.PathLike or integer, not JpegImageFile

What line in your code is triggering the exception?

Assuming one_picture is the actual filename as in “picture.jpg” you need to change the follwing line

zipObj.write(img_open+'.jpg')

Into:

zipObj.writestr(zinfo_or_arcname=one_picture, data=img_to_zip) 
# 👆 you can set zinfo_or_arcname to whatever you like, it is what the file is named within the zip file

I haven’t tested the code though, there may be also other issues. Try it out and let us know.

1 Like

YESS!! It worked! Thank you so much for your help I was really stuck in there.