How to show PIL Image in ipython notebook

前端 未结 12 1324
抹茶落季
抹茶落季 2020-12-22 23:18

This is my code

from PIL import Image
pil_im = Image.open(\'data/empire.jpg\')

I would like to do some image manipulation on it, and then s

相关标签:
12条回答
  • 2020-12-22 23:59

    much simpler in jupyter using pillow.

    from PIL import Image
    image0=Image.open('image.png')
    image0
    
    0 讨论(0)
  • 2020-12-23 00:01

    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

    0 讨论(0)
  • 2020-12-23 00:04

    I suggest following installation by no image show img.show() (from PIL import Image)

    $ sudo apt-get install imagemagick

    0 讨论(0)
  • 2020-12-23 00:05

    If you are using the pylab extension, you could convert the image to a numpy array and use matplotlib's imshow.

    %pylab # only if not started with the --pylab option
    imshow(array(pil_im))
    

    EDIT: As mentioned in the comments, the pylab module is deprecated, so use the matplotlib magic instead and import the function explicitly:

    %matplotlib
    from matplotlib.pyplot import imshow 
    imshow(array(pil_im))
    
    0 讨论(0)
  • 2020-12-23 00:07

    Based on other answers and my tries, best experience would be first installing, pillow and scipy, then using the following starting code on your jupyter notebook:

    %matplotlib inline
    from matplotlib.pyplot import imshow
    from scipy.misc import imread
    
    imshow(imread('image.jpg', 1))
    
    0 讨论(0)
  • 2020-12-23 00:08

    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)
    
    0 讨论(0)
提交回复
热议问题