When using PIL/Pillow, Jupyter Notebooks now have a display built-in that will show the image directly, with no extra fuss. Jupyter will also show the image if it is simply the last line in a cell (this has changed since the original post).
When using GenomeDiagram with Jupyter (iPython), the easiest way to display images is by converting the GenomeDiagram to a PNG image. This can be wrapped using an IPython. display. Image object to make it display in the notebook.
Python – Display Image using PIL To show or display an image in Python Pillow, you can use show() method on an image object. The show() method writes the image to a temporary file and then triggers the default program to display that image. Once the program execution is completed, the temporary file will be deleted.
When 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.
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)
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()
Use IPython display to render PIL images in a notebook.
from PIL import Image # to load images
from IPython.display import display # to display images
pil_im = Image.open('path/to/image.jpg')
display(pil_im)
I found that this is working
# source: http://nbviewer.ipython.org/gist/deeplook/5162445
from io import BytesIO
from IPython import display
from PIL import Image
def display_pil_image(im):
"""Displayhook function for PIL Images, rendered as PNG."""
b = BytesIO()
im.save(b, format='png')
data = b.getvalue()
ip_img = display.Image(data=data, format='png', embed=True)
return ip_img._repr_png_()
# register display func with PNG formatter:
png_formatter = get_ipython().display_formatter.formatters['image/png']
dpi = png_formatter.for_type(Image.Image, display_pil_image)
After this I can just do:
pil_im
But this must be last line in cell, with no print
after it
case python3
from PIL import Image
from IPython.display import HTML
from io import BytesIO
from base64 import b64encode
pil_im = Image.open('data/empire.jpg')
b = BytesIO()
pil_im.save(b, format='png')
HTML("<img src='data:image/png;base64,{0}'/>".format(b64encode(b.getvalue()).decode('utf-8')))
much simpler in jupyter using pillow.
from PIL import Image
image0=Image.open('image.png')
image0
In order to simply visualize the image in a notebook you can use display()
%matplotlib inline
from PIL import Image
im = Image.open(im_path)
display(im)
You can open an image using the Image class from the package PIL and display it with plt.imshow directly.
# First import libraries.
from PIL import Image
import matplotlib.pyplot as plt
# The folliwing line is useful in Jupyter notebook
%matplotlib inline
# Open your file image using the path
img = Image.open(<path_to_image>)
# Since plt knows how to handle instance of the Image class, just input your loaded image to imshow method
plt.imshow(img)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With