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?

Bluephoenix
  • 173
  • 1
  • 5
  • Saving it as .png does not make it a PNG file... – Benjamin Mar 14 '19 at 00:49
  • @Benjamin 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. – Bluephoenix Mar 14 '19 at 09:28

2 Answers2

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

Mr K.
  • 1,064
  • 3
  • 19
  • 22
  • It should work, but actually it doesn't, I also tried the example on their webpage: im = imageio.imread('http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png') 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. – Bluephoenix Mar 14 '19 at 10:02
  • The example works for me. You should probably check your environment configuration and connection to the Internet to see where the problem is. – Mr K. Mar 14 '19 at 11:16
  • 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. – Bluephoenix Mar 14 '19 at 13:45
-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)
ramazan polat
  • 7,111
  • 1
  • 48
  • 76
  • I got error on the open function: OSError: cannot identify image file <_io.BytesIO object at 0x10faf3e08> Do you have any idea about? – Bluephoenix Mar 13 '19 at 23:20