how to display and close image in python?

2019-06-19 15:13发布

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

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

My application display this image, but user task is to recognize object on image and write answer in terminal. If answer entered is correct user should get another image. Problem with PIL is that I cant close the image and with research the only sollution was to kill the process of image viewer, but this is not really reliable and elegant. Are there any other libraries for displaying images that have methods like .show() and .close() ?

5条回答
等我变得足够好
2楼-- · 2019-06-19 15:21

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!

Cheers,

T

查看更多
Bombasti
3楼-- · 2019-06-19 15:26

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()

enter image description here

查看更多
贼婆χ
4楼-- · 2019-06-19 15:36

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()
查看更多
一纸荒年 Trace。
5楼-- · 2019-06-19 15:42

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()
查看更多
我只想做你的唯一
6楼-- · 2019-06-19 15:47

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.

查看更多
登录 后发表回答