使用python 3.6中的matplotlib.image在python中打开.jpg图像

问题描述 投票:5回答:2

我试图在Python中使用matplotlib打开JPG图像。编辑'Spyder',Python3.6,WIndows 7

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

# Read in the image and print some stats
image = mpimg.imread(r'C:\Users\xxx\Python Code\mountain.jpg')
print('This image is: ',type(image), 
     'with dimensions:', image.shape)

但我收到以下错误...它说除了'.png'之外没有其他图像格式支持。

错误: - image = mpimg.imread(r'C:\ Users \ xxx \ Python Code \ mountain.jpg')

  File "C:\temp\Continuum\anaconda3\lib\site-packages\matplotlib\image.py", 
line 1284, in imread
    'more images' % list(handlers))

ValueError: Only know how to handle extensions: ['png']; with Pillow 
installed matplotlib can handle more images.

我经历了各种文件。其中说,为了打开'.jpg'图像,必须安装'Pillow'。如果原生matplotlib调用无法打开图像,那么它会自动回落到“枕头”上。 (如果我错了,请纠正我)

所以我安装了“枕头”。但我仍然得到错误。

你能告诉我我想念的是什么吗? (奇怪的是这个代码在另一台计算机上运行。我无法验证该机器中安装的库)

python python-3.x matplotlib image-processing pillow
2个回答
2
投票

Matplotlib需要PIL(Python Imaging Library)才能使用.jpg格式。要使用它,您需要安装Pillow(这是PIL的分支)。

使用PIP安装

pip install pillow 
      or 
pip3 install pillow

使用Conda进行安装

conda install pillow

0
投票

你必须安装PIL。确保您使用的是anaconda python发行版。转到this link或编写此命令以直接安装PIL

 conda install -c anaconda pillow 
© www.soinside.com 2019 - 2024. All rights reserved.