How to show PIL Image in ipython notebook
Updated 2021/11/17When using PIL/Pillow, Jupyter Notebooks now have a display
built-in that will show the image directly, with no extra fuss.
display(pil_im)
Jupyter will also show the image if it is simply the last line in a cell (this has changed since the original post). Thanks to answers from @Dean and @Prabhat for pointing this out.
Other MethodsFrom File
You can also use IPython's display
module to load the image. You can read more from the doc.
from IPython.display import Image
pil_img = Image(filename='data/empire.jpg')
display(pil_img)
From PIL.Image Object
As OP's requirement is to use PIL
, if you want to show inline image, you can use matplotlib.pyplot.imshow
with numpy.asarray
like this too:
from matplotlib.pyplot import imshow
import numpy as np
from PIL import Image
%matplotlib inline
pil_im = Image.open('data/empire.jpg', 'r')
imshow(np.asarray(pil_im))
If you only require a preview rather than an inline, you may just use show
like this:
pil_im = Image.open('data/empire.jpg', 'r')
pil_im.show()
How can I display an image from a file in Jupyter Notebook?
Courtesy of this post, you can do the following:
from IPython.display import Image
Image(filename='test.png')
(official docs)
How to show PIL images on the screen?
From near the beginning of the PIL Tutorial:
Once you have an instance of the Image class, you can use the methods
defined by this class to process and manipulate the image. For
example, let's display the image we just loaded:
>>> im.show()
Update:
Nowadays the Image.show()
method is formally documented in the Pillow fork of PIL along with an explanation of how it's implemented on different OSs.
How do I make 2 images appear side by side in Jupyter notebook (iPython)?
You can try using matplotlib
. You can read image to numpy
array by using mpimg.imread
(documentation) from matplotlib, then you can use subplots
(documentation) and for creating two columns for figures and finally imshow
(documetation) to display images.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib import rcParams
%matplotlib inline
# figure size in inches optional
rcParams['figure.figsize'] = 11 ,8
# read images
img_A = mpimg.imread('\path\to\img_A.png')
img_B = mpimg.imread('\path\to\img_B.png')
# display images
fig, ax = plt.subplots(1,2)
ax[0].imshow(img_A)
ax[1].imshow(img_B)
can't show an image using PIL on google colab
Instead of
im.show()
Try just
im
Colab should try to display it on its own. See example notebook
Related Topics
How to Programmatically Edit Excel Sheets
Bash: Variable in Single Quote
How to Get Each Dependent Command Execution Output Using Paramiko Exec_Command
Attribute Bold Doesn't Seem to Work in My Curses
How to Cleanly Kill Subprocesses in Python
Why Not Just Use 'Shell=True' in Subprocess.Popen in Python
Datastax Python Cassandra Driver Build Fails on Ubuntu
Where Does Python Tempfile Writes Its Files
How to Benchmark Part of Tensorflow Graph
Trying to Import Pypyodbc Module Gives Error 'Odbc Library Is Not Found. Is Ld_Library_Path Set'
Python List by Value Not by Reference
Find Nearest Value in Numpy Array
Equivalent to Gettickcount() on Linux