Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access streamlit uploaded image in opencv #888

Closed
ajinkya933 opened this issue Dec 22, 2019 · 8 comments
Closed

Access streamlit uploaded image in opencv #888

ajinkya933 opened this issue Dec 22, 2019 · 8 comments

Comments

@ajinkya933
Copy link

How do I open image in opencv. I have this code which gets uploaded image and displays it on streamlit:

uploaded_file = st.file_uploader("Choose a image file")
if uploaded_file is not None:
	image = uploaded_file.read()
	img = st.image(image, caption='Sunrise by the mountains', use_column_width=True)

My question is I want to read and save this uploaded file using:

import cv2
im1=cv2.imread(image)
im2=cv2.imwrite('out.jpg', im1)

How can I achieve this ?

@ajinkya933
Copy link
Author

uploaded_file = st.file_uploader("Upload Image")
image = Image.open(uploaded_file)
st.image(image, caption='Input', use_column_width=True)
img_array = np.array(image)
cv2.imwrite('out.jpg', cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR))

@tvst
Copy link
Contributor

tvst commented Dec 23, 2019

Nice! I was just about to post an example too 😄

For posterity, here's the way I did it:

import cv2
import numpy as np
import streamlit as st

uploaded_file = st.file_uploader("Choose a image file", type="jpg")

if uploaded_file is not None:
    # Convert the file to an opencv image.
    file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
    opencv_image = cv2.imdecode(file_bytes, 1)

    # Now do something with the image! For example, let's display it:
    st.image(opencv_image, channels="BGR")

@universewill
Copy link

can file_uploader upload multi image files at one time?
@ajinkya933 @tvst

@ajinkya933
Copy link
Author

Currently I coudnt do it. but I uploaded a zip file and and extracted it. After that you can iterate over the extracted zip file to do image transformations

@ajinkya933
Copy link
Author

You can track the issue here #905

@universewill
Copy link

That‘s great! @ajinkya933

@wbbatista
Copy link

How to get the full path of the file?

@nizarhaider
Copy link

@ajinkya933 you sir are a life saver.

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants