theta
theta

Reputation: 25631

How to plot remote image (from http url)

This must be easy, but I can't figure how right now without using urllib module and manually fetching remote file

I want to overlay plot with remote image (let's say "http://matplotlib.sourceforge.net/_static/logo2.png"), and neither imshow() nor imread() can load the image.

Any ideas which function will allow loading remote image?

Upvotes: 19

Views: 24496

Answers (4)

Paulo Belo
Paulo Belo

Reputation: 4457

pyplot.imread for URLs is deprecated

Passing a URL is deprecated. Please open the URL for reading and pass the result to Pillow, e.g. with np.array(PIL.Image.open(urllib.request.urlopen(url))).

Matplotlib suggests using PIL instead. I prefer using imageio as sugested by SciPy:

imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead.

imageio.imread(uri, format=None, **kwargs)

Reads an image from the specified file. Returns a numpy array, which comes with a dict of meta data at its ‘meta’ attribute.

Note that the image data is returned as-is, and may not always have a dtype of uint8 (and thus may differ from what e.g. PIL returns).

Example:

import matplotlib.pyplot as plt
from imageio import imread

url = "http://matplotlib.sourceforge.net/_static/logo2.png"
img = imread(url)
plt.imshow(img)

Upvotes: 3

crowdy
crowdy

Reputation: 319

you can do it with this code;

from matplotlib import pyplot as plt
a = plt.imread("http://matplotlib.sourceforge.net/_static/logo2.png")
plt.imshow(a)
plt.show()

Upvotes: 15

Julian
Julian

Reputation: 2620

This works for me in a notebook with python 3.5:

from skimage import io
import matplotlib.pyplot as plt

image = io.imread(url)
plt.imshow(image)
plt.show()

Upvotes: 19

Daniel
Daniel

Reputation: 27639

It is easy indeed:

import urllib2
import matplotlib.pyplot as plt

# create a file-like object from the url
f = urllib2.urlopen("http://matplotlib.sourceforge.net/_static/logo2.png")

# read the image file in a numpy array
a = plt.imread(f)
plt.imshow(a)
plt.show()

Upvotes: 21

Related Questions