How to display and close an image using Python?

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

im = Image.open("image.jpg") im.show() 

My application displays this image, but the task of the user is to recognize the object in the image and write the answer in the terminal. If the answer is entered correctly, the user should get a different image. The problem with PIL is that I cannot close the image, and with the help of research, the only solution was to kill the image viewing process, but this is not entirely reliable and elegant. Are there any other libraries for displaying images that have methods like .show () and .close ()?

+5
source share
5 answers

Just open any viewer / image editor in a separate process and kill it as soon as the user answers your question, for example.

 from PIL import Image import subprocess p = subprocess.Popen(["display", "/tmp/test.png"]) raw_input("Give a name for image:") p.kill() 
+3
source

The terminal is designed to work with a linear flow of commands, that is, it asks a question, user answers, and then can ask another question. What you are trying to do here is to make a terminal to do two things, show the image and at the same time ask the user a question. There are two things you can do for this:

Multiprocessing

You can start a new thread / process and make PIL display the image using this thread, and meanwhile in the first thread / process ask the user a question. Then, after user response, you can close another thread / process. You can see the Python threading module ( link ) for more information on how you can do this.

GUI

Instead of making your user interface in a terminal, make a simple GUI application using a convenient infrastructure. I personally like PyQt4 . Qt is a very powerful GUI toolkit, and PyQt4 is a wrapper for it. If you are creating a graphical interface, then what you are doing is pretty trivial.

+2
source

A bit late for the party, but (as an angry data expert who really can't be bothered learning about GUI programming for the sake of displaying an image), I can probably speak for a few other people who would like to find a simpler solution for this one. I decided to work a bit by expanding the Anurag solution:

Create a second python script (let it be called 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 force the main script to temporarily save the image you want to open with imviewer.py, and then overwrite it with the next image, etc.

Hope this helps someone in this matter!

Hurrah,

T

+2
source

It may be superfluous, but for me the simplest and most reliable solution was to simply use matplotlib , since it correctly tracks the drawings that it creates, for example

 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
source

Not all graphical interfaces are difficult to use.

Here is a single line solution using PySimpleGUI. Usually I do not write this in one line, but since it is disposable, it may not need to be added, then everything is in order.

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

enter image description here

0
source

All Articles