I demo this repo , and I wanna set a button to download a Processed image. .
I try cv2.imwrite(‘messigray.png’,img) but not work.
I demo this repo , and I wanna set a button to download a Processed image. .
I try cv2.imwrite(‘messigray.png’,img) but not work.
Hi @sevaroy, welcome to the Streamlit community!
This thread shows several examples of how to create a download link. In your code snippet, you are writing the image to a folder somewhere. So you either need to be able to pass that file location to a button for people to click, or you need to pass the actual bytes of the file to your download link.
Thank you! This works for me.
Use the following function to download the image file, you can same code for every file, just you need to change the extension and saving technique according to a different file.
This function will create the link for downloading the Image file
def get_image_download_link(img,filename,text):
buffered = BytesIO()
img.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue()).decode()
href = f'<a href="data:file/txt;base64,{img_str}" download="{filename}">{text}</a>'
return href
Function Call
## Original image came from cv2 format, fromarray convert into PIL format
result = Image.fromarray(original_image)
st.markdown(get_image_download_link(result,img_file.name,'Download '+img_file.name), unsafe_allow_html=True)
Hey all ,
We now have Download Button natively supported in Streamlit via the 0.88.0 release with st.download_button
!
To use, upgrade to the latest version:
pip install --upgrade streamlit
Check out these helpful links:
Hello @randyzwitch. Would you have any suggestions on how to download a 3D tiff image? (Here the third dimension may have a size of more than 3, so it is not necessarily an RGB image).
I mentioned this question here as well for completion.
Hello Jessica sorry to hijack the discussion I was wonder possible you can point me out to solved this issue.
st.write("Preview")
#openCV image result cropped_image which is an np array
st.image(cropped_image)
#cropped_image converted to PIL image color
result = Image.fromarray(cropped_image.astype('uint8'), 'RGB')
img = Image.open(result)
btn = st.download_button(
label="Download image",
data=img,
file_name="imagename.png",
mime="image/png")
I want to use st.download_button to download the image result I know I cannot no use the cropped_image result since it is a np array. I converted the image array to a PIL image but I don’t know how I can from here get the result image filename. Could you please give me some ideas how to solve this issue.
hi, i have the same issue. how did you solved?
In case someone needs a solution, what I did was to convert the img (in my case it was numpy array) to io.BufferedReader.
im_rgb = im_bgr[:, :, [2, 1, 0]] #numpy.ndarray
ret, img_enco = cv2.imencode(".png", im_rgb) #numpy.ndarray
srt_enco = img_enco.tostring() #bytes
img_BytesIO = BytesIO(srt_enco) #_io.BytesIO
img_BufferedReader = BufferedReader(img_BytesIO) #_io.BufferedReader
cols_srcnn[i].download_button(
label="Download",
data=img_BufferedReader,
file_name="srcnn_img_"+str(i)+".png",
mime="image/png"
)
from io import BytesIO
buf = BytesIO()
img.save(buf, format="JPEG")
byte_im = buf.getvalue()
Now you can use the st.download_button
btn = col.download_button(
label="Download Image",
data=byte_im,
file_name="imagename.png",
mime="image/jpeg",
)
Hello.
I am developing an App that from a library generates an image but it is not being saved as a png or jpg file, it is just being displayed but I need that the person using the app can download it through a button of some kind. Is this possible?
This is what the image looks like:
What library do you use? Can you show your code that generates the image?
Hii Javier_Jaramilo
this code is working good but the 3rd “img.save(buf, format=“JPEG”)” is not working
this is showing me error :- NameError: name ‘img’ is not defined
please help me
hiii
you can use this code to download the image
from io import BytesIO
buf = BytesIO()
img.save(buf, format=“JPEG”)
byte_im = buf.getvalue()
btn = st.download_button(
label=“Download Image”,
data=byte_im,
file_name=“graph.jpg”,
mime=“image/jpg”,
)
but this line code " img.save(buf, format=“JPEG”)" is not working
what dose the first two show ?
t
Hey Pawan, sorry I was a little late. Well maybe the reason img.save(buf, format=“JPEG”)" won’t work it is becuase thats you image.
So for example:
img = “path/example.jpg” # or any other format and them when you have the image in the buffer the rest of the code get execute.
Or in other work you need to have you image in the buffer first to further processing.
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.