192

I tried to use IPython.display with the following code:

from IPython.display import display, Image
display(Image(filename='MyImage.png'))

I also tried to use matplotlib with the following code:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
plt.imshow(mpimg.imread('MyImage.png'))

In both cases, nothing is displayed, not even an error message.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
FiReTiTi
  • 5,597
  • 12
  • 30
  • 58

11 Answers11

362

If you are using matplotlib and want to show the image in your interactive notebook, try the following:

%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('your_image.png')
imgplot = plt.imshow(img)
plt.show()
David Medinets
  • 5,160
  • 3
  • 29
  • 42
Joe Bathelt
  • 5,459
  • 2
  • 15
  • 14
  • Thank you for your answer, it almost works :-) The image is displayed, but the gray levels are replaced using a fire lut. Any idea? – FiReTiTi Feb 09 '16 at 09:14
  • 3
    Try defining the colormap explicitly: plt.imshow(lum_img, cmap="grey") – Joe Bathelt Feb 09 '16 at 09:16
  • 3
    In my understanding, the last plt.show() command produces the display. Before that, you could add to the plot, e.g. by adding layers to the image. I recommend the matplotlib image tutorial as a reference: http://matplotlib.org/users/image_tutorial.html – Joe Bathelt Feb 09 '16 at 09:18
  • 8
    Thanks for your answers, now it works perfectly! Just a little details for the potential readers, it "gray" instead of "grey". – FiReTiTi Feb 09 '16 at 09:19
  • 1
    You could avoid using `matplotlib.image`: img = plt.imread('your_image.png') – Nir May 06 '20 at 09:41
  • @perfo he meant the argument 'gray' rather than how to spell the word. """ cmaps['Sequential (2)'] = [ 'binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink', 'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia', 'hot', 'afmhot', 'gist_heat', 'copper'] """ – Ari Sep 10 '20 at 17:07
  • @Ari. I guess so. – perfo Sep 11 '20 at 19:45
  • If the image's resolution seems lower than the original, add this before the imread : `plt.figure(dpi=600)`. More detail [here](https://stackoverflow.com/questions/39870642/matplotlib-how-to-plot-a-high-resolution-graph) – kotchwane Nov 22 '20 at 15:16
55

If you use matplotlib, you need to show the image using plt.show() unless you are not in interactive mode. E.g.:

plt.figure()
plt.imshow(sample_image) 
plt.show()  # display it
mrk
  • 8,059
  • 3
  • 56
  • 78
MaxPowers
  • 5,235
  • 2
  • 44
  • 69
55

In a much simpler way, you can do the same using

from PIL import Image

image = Image.open('image.jpg')
image.show()
Dipanshu Mahla
  • 152
  • 1
  • 16
the_unknown_spirit
  • 2,518
  • 7
  • 34
  • 56
12

Your first suggestion works for me

from IPython.display import display, Image
display(Image(filename='path/to/image.jpg'))
Harry Moreno
  • 10,231
  • 7
  • 64
  • 116
9

Using opencv-python is faster for more operation on image:

import cv2
import matplotlib.pyplot as plt

im = cv2.imread('image.jpg')
im_resized = cv2.resize(im, (224, 224), interpolation=cv2.INTER_LINEAR)

plt.imshow(cv2.cvtColor(im_resized, cv2.COLOR_BGR2RGB))
plt.show()
Qhan
  • 131
  • 1
  • 3
4

It's simple Use following pseudo code

from pylab import imread,subplot,imshow,show

import matplotlib.pyplot as plt

image = imread('...')  // choose image location

plt.imshow(image)

plt.show() // this will show you the image on console.

4

Using Jupyter Notebook, the code can be as simple as the following.

%matplotlib inline
from IPython.display import Image
Image('your_image.png')

Sometimes you might would like to display a series of images in a for loop, in which case you might would like to combine display and Image to make it work.

%matplotlib inline
from IPython.display import display, Image
for your_image in your_images:
    display(Image('your_image'))
Fei Yao
  • 1,502
  • 10
  • 10
3

Your code:

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

What it should be:

plt.imshow(mpimg.imread('MyImage.png'))
File_name = mpimg.imread('FilePath')
plt.imshow(FileName)
plt.show()

you're missing a plt.show() unless you're in Jupyter notebook, other IDE's do not automatically display plots so you have to use plt.show() each time you want to display a plot or made a change to an existing plot in follow up code.

Dmitriy Fialkovskiy
  • 3,065
  • 8
  • 32
  • 47
2
import IPython.display as display
from PIL import Image
image_path = 'my_image.jpg'
display.display(Image.open(image_path))
Rajan saha Raju
  • 794
  • 7
  • 13
1

Solution for Jupyter notebook PIL image visualization with arbitrary number of images:

def show(*imgs, **kwargs):
    '''Show in Jupyter notebook one or sequence of PIL images in a row. figsize - optional parameter, controlling size of the image.
    Examples:
    show(img)
    show(img1,img2,img3)
    show(img1,img2,figsize=[8,8])
    '''
    
    if 'figsize' not in kwargs:
        figsize = [9,9]
    else:
        figsize = kwargs['figsize']
    
    fig, ax = plt.subplots(1,len(imgs),figsize=figsize)
    if len(imgs)==1:
        ax=[ax]
    
    for num,img in enumerate(imgs):
        ax[num].imshow(img)
        ax[num].axis('off')
        
    tight_layout()
Apogentus
  • 6,371
  • 6
  • 32
  • 33
0

To display images in python, you can use a tool that I made.

  1. Install python3:

    • apt install python3
  2. Clone the repo:

  3. Install python required libs:

    • cd print-image-in-terminal
    • pip3 install numpy
    • pip3 install pillow

Usage: python3 print_image.py [path to image]

Examples: python3 print_image.py sinchan.jpg

Pausi
  • 134
  • 2
  • 7