imread不适用于在线jpg文件

时间:2016-12-18 10:13:41

标签: python matplotlib anaconda jupyter-notebook

我使用的是Jupyter的Anaconda发行版,我在使用matplotlib.pylab.imread从互联网上读取.jpg文件时遇到了麻烦,即使阅读本地jpg文件也能正常工作。有任何解决这个问题的方法吗?我在下面包含了我的代码和错误日志

import matplotlib.pylab as plt

LIFE = plt.imread("LIFE.jpg", format="jpg") # local file
LIFE = plt.imread("http://2neatmagazines.com/Life-Magazine-Covers/1969/Life-Magazine-1969-08-11.jpg", format="jpg")

---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-41-065a192e7dc8> in <module>()
      3 
      4 LIFE = plt.imread("LIFE.jpg", format="jpg")
----> 5 LIFE = plt.imread("http://2neatmagazines.com/Life-Magazine-Covers/1969/Life-Magazine-1969-08-11.jpg", format="jpg")

C:\Program Files\Anaconda2\lib\site-packages\matplotlib\pyplot.py in imread(*args, **kwargs)
   2295 @docstring.copy_dedent(_imread)
   2296 def imread(*args, **kwargs):
-> 2297     return _imread(*args, **kwargs)
   2298 
   2299 

C:\Program Files\Anaconda2\lib\site-packages\matplotlib\image.py in imread(fname, format)
   1304 
   1305     if ext not in handlers:
-> 1306         im = pilread(fname)
   1307         if im is None:
   1308             raise ValueError('Only know how to handle extensions: %s; '

C:\Program Files\Anaconda2\lib\site-packages\matplotlib\image.py in pilread(fname)
   1282         except ImportError:
   1283             return None
-> 1284         image = Image.open(fname)
   1285         return pil_to_array(image)
   1286 

C:\Program Files\Anaconda2\lib\site-packages\PIL\Image.py in open(fp, mode)
   2278             filename = str(fp.resolve())
   2279     if filename:
-> 2280         fp = builtins.open(filename, "rb")
   2281 
   2282     try:

IOError: [Errno 22] invalid mode ('rb') or filename: 'http://2neatmagazines.com/Life-Magazine-Covers/1969/Life-Magazine-1969-08-11.jpg'

注意:我尝试了matplotlib.image.imreadscipy.misc.imread,但都失败并出现同样的错误

SOLUTION:

我被引导到this SO answer一个类似的问题,这解决了我的问题。请注意,对于JPG文件,您必须添加format参数,例如

a = plt.imread(f, format="jpg")

0 个答案:

没有答案