Converting the response of Python get request(jpg content) in Numpy Array
Asked Answered
T

2

7

The workflow of my function is the following:

  • retrieve a jpg through python get request
  • save image as png (even though is downloaded as jpg) on disk
  • use imageio to read from disk image and transform it into numpy array
  • work with the array

This is what I do to save:

response = requests.get(urlstring, params=params)
      if response.status_code == 200:
            with open('PATH%d.png' % imagenumber, 'wb') as output:
                output.write(response.content)

This is what I do to load and transform png into np.array

imagearray = im.imread('PATH%d.png' % imagenumber)

Since I don't need to store permanently what I download I tried to modify my function in order to transform the response.content in a Numpy array directly. Unfortunately every imageio like library works in the same way reading a uri from the disk and converting it to a np.array.

I tried this but obviously it didn't work since it need a uri in input

response = requests.get(urlstring, params=params)
imagearray = im.imread(response.content))

Is there any way to overcome this issue? How can I transform my response.content in a np.array?

Tryst answered 13/3, 2019 at 22:19 Comment(2)
Saving it as .png does not make it a PNG file...Haply
@Haply I know, but actually this is not the issue. I just followed the instructions of the API I’m using to download the images; they use png format to download, even though they send a jpg for the image I specifically need.Tryst
S
5

imageio.imread is able to read from urls:

import imageio

url = "https://example_url.com/image.jpg"

# image is going to be type <class 'imageio.core.util.Image'>
# that's just an extension of np.ndarray with a meta attribute

image = imageio.imread(url)

You can look for more information in the documentation, they also have examples: https://imageio.readthedocs.io/en/stable/examples.html

Son answered 14/3, 2019 at 1:52 Comment(3)
It should work, but actually it doesn't, I also tried the example on their webpage: im = imageio.imread('upload.wikimedia.org/wikipedia/commons/d/de/…) It shows an error that looking to the traceback is correlated with ssl certificate; I tried both formulas 'http' and 'https' with the same error result.Tryst
The example works for me. You should probably check your environment configuration and connection to the Internet to see where the problem is.Son
I found the issue, in Python 3.6+ on macOS X ssl certificates need to be installed from the package given in the Python installer. Once I did that everything worked fine. I only had to add this part inside the call to make it work: imagearray = im.imread(im.core.urlopen(url).read(), '.jpg') Maybe you can add it to your post, in case any need the same solution.Tryst
O
-2

You can use BytesIO as file to skip writing to an actual file.

bites = BytesIO(base64.b64decode(response.content))

Now you have it as BytesIO, so you can use it just like a file:

img = Image.open(bites)
img_np = np.array(im)
Oh answered 13/3, 2019 at 23:0 Comment(1)
I got error on the open function: OSError: cannot identify image file <_io.BytesIO object at 0x10faf3e08> Do you have any idea about?Tryst

© 2022 - 2024 — McMap. All rights reserved.