How to show PIL Image in ipython notebook

前端 未结 12 1388
抹茶落季
抹茶落季 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-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

提交回复
热议问题