So I am trying to use a download button to download an image after a filter has been applied to it.
The idea is to take an uploaded image, make a Pencil Sketch of the uploaded image and then download the sketched image. I am able to display the pencil sketched image well enough as you can see below:
I cannot however download the pencil sketched image. The output is a file and not an image.
Please ignore the label on the button, in case it is misleading.
Here is how I have tried so far:
# Convert Image
def save_image(byteImage):
bytesImg = io.BytesIO(byteImage)
imgFile = Image.open(bytesImg)
return imgFile
# Check if the file has been uploaded
if uploaded_file is not None:
file = uploaded_file.read()
filename = uploaded_file.name.split('.')[0]
path = save_image(file)
st.title(option)
# st.markdown('## Original Image')
col1, col2, col3 = st.columns([1, 1, 1])
col2.image(path, width=500, caption='Uploaded Image')
file_bytes = np.asarray(bytearray(file), dtype=np.uint8)
opencv_image = cv2.imdecode(file_bytes, 1)
if option == 'Pencil Sketch':
sketch = PencilSketch(opencv_image)
col1, col2, col3 = st.columns([1, 1, 1])
col2.image(sketch, width=500, caption='Pencil Sketch')
col1, col2, col3 = st.columns([1, 1, 1])
btn = col2.download_button(
label="Download {}".format(filename),
data=file,
file_name=filename,
mime="image/png",
)
Also, is there a way that I can center the download button further, so that it is straight at the center below the image? I am aware that Streamlit does not support nested columns, so I am not sure about the other ways to do this.
Thank you!