Answer a question

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:

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 can't close the image and with research the only solution 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() ?

Answers

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

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐