How to show PIL Image in ipython notebook

前端 未结 12 629
悲&欢浪女
悲&欢浪女 2020-12-22 23:08

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:41

    You can use IPython's Module: display 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)
    

    updated

    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()
    
    0 讨论(0)
  • 2020-12-22 23:41

    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)
  • 2020-12-22 23:55

    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)
    
    0 讨论(0)
  • 2020-12-22 23:58

    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)
    
    0 讨论(0)
  • 2020-12-22 23:59

    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:01

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