question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access streamlit uploaded image in opencv

See original GitHub issue

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 ?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

21reactions
tvstcommented, 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")
14reactions
ajinkya933commented, Dec 23, 2019
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))
Read more comments on GitHub >

github_iconTop Results From Across the Web

File Uploader File to OpenCV imread - Using Streamlit
How can I pass image file from file_uploader() to opencv's imread()? ... It returns a 'stream' class variable to access it.
Read more >
How to Build an OpenCV Web App with Streamlit
In this tutorial, you'll learn how to easily convert an OpenCV project into a web app that you can showcase. You'll use a...
Read more >
Working with File Uploads in Streamlit Python - JCharisTech
In this tutorial we will be exploring streamlit file upload feature. ... Hence if it is an image you will have to use...
Read more >
Create A Photo Editing App Using Streamlit
Create A Photo Editing App Using Streamlit — Surprisingly Easy and Fun · Introduction · Pre-requisite · Launch Streamlit · Import Libraries ·...
Read more >
Working with File Uploads In Streamlit Python - YouTube
In this indepth tutorial we will be working with the file uploads feature of streamlit - how to process Images,PDF,Docx,Txt,CSV etc.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found