Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 200
Tanay Avatar asked Jan 10 '18 11:01

Tanay


People also ask

Which library of Python is used to see the image on a plot?

Matplotlib is a widely used data visualization library in python. This article illustrates how to display, modify and save an image using the 'matplotlib' library. We will see how to use the 'image' module as it makes working with images quite easy.


1 Answers

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
like image 153
Swapnil Avatar answered Sep 18 '22 17:09

Swapnil