Display an image with Python

前端 未结 9 1589
后悔当初
后悔当初 2020-12-23 00:21

I tried to use IPython.display with the following code:

from IPython.display import display, Image
display(Image(filename=\'MyImage.png\'))

相关标签:
9条回答
  • 2020-12-23 00:45

    Your code:

    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    

    What it should be:

    plt.imshow(mpimg.imread('MyImage.png'))
    File_name = mpimg.imread('FilePath')
    plt.imshow(FileName)
    plt.show()
    

    you're missing a plt.show() unless you're in Jupyter notebook, other IDE's do not automatically display plots so you have to use plt.show() each time you want to display a plot or made a change to an existing plot in follow up code.

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

    If you are using matplotlib and want to show the image in your interactive notebook, try the following:

    %pylab inline
    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    img = mpimg.imread('your_image.png')
    imgplot = plt.imshow(img)
    plt.show()
    
    0 讨论(0)
  • 2020-12-23 00:48
    import IPython.display as display
    from PIL import Image
    image_path = 'my_image.jpg'
    display.display(Image.open(image_path))
    
    0 讨论(0)
  • 2020-12-23 00:50

    In a much simpler way, you can do the same using

    from PIL import Image
    
    image = Image.open('image.jpg')
    image.show()
    
    0 讨论(0)
  • 2020-12-23 00:50

    Using opencv-python is faster for more operation on image:

    import cv2
    import matplotlib.pyplot as plt
    
    im = cv2.imread('image.jpg')
    im_resized = cv2.resize(im, (224, 224), interpolation=cv2.INTER_LINEAR)
    
    plt.imshow(cv2.cvtColor(im_resized, cv2.COLOR_BGR2RGB))
    plt.show()
    
    0 讨论(0)
  • 2020-12-23 00:56

    Your first suggestion works for me

    from IPython.display import display, Image
    display(Image(filename='path/to/image.jpg'))
    
    0 讨论(0)
提交回复
热议问题