[python] Display an image with Python

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.

This question is related to python matplotlib ipython

The answer is


Your first suggestion works for me

from IPython.display import display, Image
display(Image(filename='path/to/image.jpg'))

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()

import IPython.display as display
from PIL import Image
image_path = 'my_image.jpg'
display.display(Image.open(image_path))

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'))

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.


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

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()

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

from PIL import Image

image = Image.open('image.jpg')
image.show()

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.


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to matplotlib

"UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm How to increase image size of pandas.DataFrame.plot in jupyter notebook? How to create a stacked bar chart for my DataFrame using seaborn? How to display multiple images in one figure correctly? Edit seaborn legend How to hide axes and gridlines in Matplotlib (python) How to set x axis values in matplotlib python? How to specify legend position in matplotlib in graph coordinates Python "TypeError: unhashable type: 'slice'" for encoding categorical data Seaborn Barplot - Displaying Values

Examples related to ipython

How to increase image size of pandas.DataFrame.plot in jupyter notebook? Importing .py files in Google Colab Selection with .loc in python IOPub data rate exceeded in Jupyter notebook (when viewing image) Purpose of "%matplotlib inline" Installing a pip package from within a Jupyter Notebook not working convert json ipython notebook(.ipynb) to .py file In which conda environment is Jupyter executing? How to make inline plots in Jupyter Notebook larger? %matplotlib line magic causes SyntaxError in Python script