How can I display an image from a file in Jupyter Notebook?

前端 未结 11 1106
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 01:03

I would like to use an IPython notebook as a way to interactively analyze some genome charts I am making with Biopython\'s GenomeDiagram module. While there is extensive doc

相关标签:
11条回答
  • 2020-11-28 01:23

    Another option for plotting inline from an array of images could be:

    import IPython
    def showimg(a):
        IPython.display.display(PIL.Image.fromarray(a))
    

    where a is an array

    a.shape
    (720, 1280, 3)
    
    0 讨论(0)
  • 2020-11-28 01:24

    Courtesy of this page, I found this worked when the suggestions above didn't:

    import PIL.Image
    from cStringIO import StringIO
    import IPython.display
    import numpy as np
    def showarray(a, fmt='png'):
        a = np.uint8(a)
        f = StringIO()
        PIL.Image.fromarray(a).save(f, fmt)
        IPython.display.display(IPython.display.Image(data=f.getvalue()))
    
    0 讨论(0)
  • 2020-11-28 01:27

    Note, until now posted solutions only work for png and jpg!

    If you want it even easier without importing further libraries or you want to display an animated or not animated GIF File in your Ipython Notebook. Transform the line where you want to display it to markdown and use this nice short hack!

    ![alt text](test.gif "Title")
    
    0 讨论(0)
  • 2020-11-28 01:29

    Courtesy of this post, you can do the following:

    from IPython.display import Image
    Image(filename='test.png') 
    

    (official docs)

    0 讨论(0)
  • 2020-11-28 01:30

    If you are trying to display an Image in this way inside a loop, then you need to wrap the Image constructor in a display method.

    from IPython.display import Image, display
    
    listOfImageNames = ['/path/to/images/1.png',
                        '/path/to/images/2.png']
    
    for imageName in listOfImageNames:
        display(Image(filename=imageName))
    
    0 讨论(0)
  • 2020-11-28 01:35

    This will import and display a .jpg image in Jupyter (tested with Python 2.7 in Anaconda environment)

    from IPython.display import display
    from PIL import Image
    
    
    path="/path/to/image.jpg"
    display(Image.open(path))
    

    You may need to install PIL

    in Anaconda this is done by typing

    conda install pillow
    
    0 讨论(0)
提交回复
热议问题