简体   繁体   中英

open a .jpg image in python using matplotlib.image in python 3.6

I am trying to open a JPG image using matplotlib in Python. Editor '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)

But I am getting the following error... It says that except '.png' no other image format is supported.

Error :-- 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.

I went through various documentations. Which says that, in order to open a '.jpg' image, 'Pillow' must be installed. If native matplotlib call fails to open a image then it automatically falls back on 'pillow'. (correct me if I am wrong)

So I installed 'Pillow'. But I am still getting the error.

Can you tell me what am I Missing ? (Strange thing is this same code is running in another computer. I have no way to verify what library is installed in that machine)

Matplotlib requires PIL( Python Imaging Library ) to work with .jpg format. To use it you need to install Pillow ( which is the fork of PIL ).

Installation Using PIP

pip install pillow 
      or 
pip3 install pillow

Installation Using Conda

conda install pillow

You have to install PIL. Make sure you are using anaconda python distro. Go to this link or write this command to install PIL directly

 conda install -c anaconda pillow 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM