How do I display and close an image with Python?

后端 未结 5 687
佛祖请我去吃肉
佛祖请我去吃肉 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:14

    A little late to the party, but (as a disgruntled data scientist who really can't be bothered to learn gui programming for the sake of displaying an image) I can probably speak for several other folks who would like to see an easier solution for this. I figured out a little work around by expanding Anurag's solution:

    Make a second python script (let's call it 'imviewer.py'):

    from skimage.viewer import ImageViewer
    from skimage.io import imread
    
    img = imread('image.png') #path to IMG
    view = ImageViewer(img)
    view.show()
    

    Then in your main script do as Anurag suggested:

    import subprocess
    p = subprocess.Popen('python imviewer.py')
    #your code
    p.kill()
    

    You can make the main script save the image you want to open with 'imviewer.py' temporarily, then overwrite it with the next image etc.

    Hope this helps someone with this issue!

    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2021-01-12 04:30

    Just open any image viewer/editor in a separate process and kill it once user has answered your question e.g.

    from PIL import Image
    import subprocess
    
    p = subprocess.Popen(["display", "/tmp/test.png"])
    raw_input("Give a name for image:")
    p.kill()
    
    0 讨论(0)
  • 2021-01-12 04:32

    Not all GUIs are difficult to use.

    Here is a single-line solution using PySimpleGUI. Normally I wouldn't write it as a single line, but since it's a one-off, perhaps doesn't need adding to, then it's OK to do.

    import PySimpleGUI as sg
    
    sg.Window('My window').Layout([[ sg.Image('PySimpleGUI.png') ]]).Read()
    

    0 讨论(0)
  • 2021-01-12 04:37

    Terminal is meant to deal with linear command flow - meaning it asks a question, user answers, and then it can ask a different question. What you are trying to do here is for terminal to do two things, show an image and at the same time ask user a question. To do this you can do two of either things:

    Multiprocessing

    You can start a new thread/process and make PIL show the image using that thread, and meanwhile in the first thread/process ask a user a question. Then after the user answers, you can close the other thread/process. You can take a look at Python's threading module (link) for more information on how you can do that.

    GUI

    Instead of making your user interface in terminal, make a simple GUI application using whatever framework you are comfortable. I personally like PyQt4. Qt is very powerful GUI development toolkit and PyQt4 is a wrapper for it. If you make a GUI, then what you are tyring to do is rather trivial.

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