1

I want to load to opencv an image that I've downloaded. I would like to avoid saving it to a file. I can perfectly download the image:

page_html = requests.get("http://my_web_page.com")

image_src = parse.search('<img id="my_image" src="{}"', page_html.content.decode('utf-8'))[0]
if image_src:
    image = requests.get("http://my_web_age.com" + image_src).content

And I can save it to a file and check it with my file explorer:

with open('main_image.png', 'wb') as file:
    file.write(image.content)

But if I try to directly load the image from the content of the variable, it doesn't work:

cv2_image = cv2.imread(image.content, cv2.IMREAD_COLOR)

I get:

SystemError: <built-in function imread> returned NULL without setting an error

Reading from the file works nevertheless, but is it possible to skip this step? The data is already in the variable, so it should be possible.

3
  • stackoverflow.com/questions/141449/… This sounds like it may do what you need. Create a file object from a string. Commented Oct 11, 2019 at 10:18
  • @JammyDodger but it is not a string, is a bytes object. Do you think is the right direction? And it seems to be only for python2? Commented Oct 11, 2019 at 10:24
  • i see in the docs that cv2.imread takes filepath as first parameter Commented Oct 11, 2019 at 10:41

1 Answer 1

2

You can use numpy's frombuffer to convert the data to integers. imdecode then turns it into an image array you use with opencv.

Working example:

import cv2
import numpy as np
import requests

# perform request
response =  requests.get('https://opencv-python-tutroals.readthedocs.io/en/latest/_images/messiup.jpg').content
# convert to array of ints
nparr = np.frombuffer(response, np.uint8)
# convert to image array
img = cv2.imdecode(nparr,cv2.IMREAD_UNCHANGED)

# showimage
cv2.imshow("Res", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.