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.

Blasco
  • 1,607
  • 16
  • 30
  • https://stackoverflow.com/questions/141449/how-do-i-wrap-a-string-in-a-file-in-python This sounds like it may do what you need. Create a file object from a string. – clubby789 Oct 11 '19 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? – Blasco Oct 11 '19 at 10:24
  • i see in the docs that `cv2.imread` takes filepath as first parameter – Kadmillos Oct 11 '19 at 10:41

1 Answers1

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()
J.D.
  • 4,511
  • 2
  • 7
  • 20