如何绘制远程图像(从http url)

2024-05-12 17:07:13 发布

您现在位置:Python中文网/ 问答频道 /正文

这一定很简单,但是如果不使用urllib模块并手动获取远程文件,我现在想不通

我想用远程图像覆盖plot(比如“http://matplotlib.sourceforge.net/_static/logo2.png”),而imshow()imread()都不能加载图像。

你知道哪个功能可以加载远程图像吗?


Tags: 模块文件图像httpnet远程plotpng
3条回答

你可以用这个代码来做

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

这在python 3.5的笔记本中对我很有用:

from skimage import io
import matplotlib.pyplot as plt

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

确实很容易:

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

相关问题 更多 >