5

Using python, I would like to download a Jpeg image given a URL and stored it in a numpy array for processing later. What library should I use?

martineau
  • 119,623
  • 25
  • 170
  • 301
koayst
  • 2,095
  • 3
  • 17
  • 16
  • Does this answer your question? [Python - how to read an image from a URL?](https://stackoverflow.com/questions/40911170/python-how-to-read-an-image-from-a-url) – fabianegli Jan 13 '20 at 21:19
  • Try scikit-image: https://stackoverflow.com/a/40911860/6018688 i.e. `from skimage import io; io.imread(url)` – fabianegli Jan 13 '20 at 21:22

2 Answers2

6

There are a number of libraries that can be used for this task. The one that I've used in the past is from scikit-image:

import skimage

image_filename = "http://example.com/example.jpg"
image_numpy = skimage.io.imread( image_filename )
jss367
  • 4,759
  • 14
  • 54
  • 76
Jason K Lai
  • 1,500
  • 5
  • 15
4

You can use either request library or wgetlibrary to handle download the image. And then use cv2 to open as numpy array

wget is faster, pip install wget

Example (wget):

import cv2, wget
image_url = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png'
filename = wget.download(image_url)
np_image = cv2.imread(filename)

Example (request)

import requests, io, cv2
import numpy as np
from PIL import Image
response = requests.get('https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png')
bytes_im = io.BytesIO(response.content)
cv_im = cv2.cvtColor(np.array(Image.open(bytes_im)), cv2.COLOR_RGB2BGR)
Robin XW
  • 71
  • 6