How do I display and close an image with Python?

后端 未结 5 721
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 04:08

I would like to display an image with Python and close it after user enters the name of the image in terminal. I use PIL to display image, here is the code:

         


        
5条回答
  •  醉话见心
    2021-01-12 04:19

    Might be an overkill, but for me the easiest and most robust solution was just to use matplotlib as it properly keeps track of the figures it creates, e.g. :

    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    
    imgplot = plt.imshow(mpimg.imread('animal.png'))
    plt.ion()
    plt.show()
    animal_name = raw_input("What is the name?: ")
    plt.close()
    

提交回复
热议问题