19

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?

4 Answers 4

21

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()
Sign up to request clarification or add additional context in comments.

4 Comments

Well I was hoping that MPL can do this w/o urllib, but maybe it can't. I'll wait a bit more and if it's indeed impossible I'll mark you answer
Looking at the docs I don't think it's possible. And it's only 2 lines extra here, no big deal.
For python 3, import urllib instead of urllib2 and call urllib.request.urlopen instead of urllib2.urlopen.
Also, imread cannot guess the filetype from a stream like this. It defaults to PNG so this works for PNGs only. For non-PNG files, one needs to pass the format parameter to imread (e.g. format='jpg'). This requires additional work to extract the filetype from the url.
19

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()

4 Comments

I got ValueError: invalid PNG header for the urllib2 solution but this worked well for me
To get skimage, pip install scikit-image
Worked for me! (base:conda)
this didn't work for me because skimage.io prepends my current working directory to the S3 url
15

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()

3 Comments

How would you suggest handling 403 errors?
this doesn't work for S3 urls: URLError: <urlopen error unknown url type: s3>
Directly reading images from URLs is deprecated, it is recommended to open the URL for reading and pass the result to Pillow, e.g. with np.array(PIL.Image.open(urllib.request.urlopen(url)))
3

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.