如何使用Python显示和关闭图像? [英] How do I display and close an image with Python?

查看:2662
本文介绍了如何使用Python显示和关闭图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Python显示图像并在用户在终端中输入图像名称后将其关闭.我使用PIL显示图像,这是代码:

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

我的应用程序显示此图像,但是用户任务是识别图像上的对象并在终端中写入答案.如果输入的答案正确,则用户应获得另一张图像. PIL的问题是我无法关闭图像,而通过研究,唯一的解决方案是取消图像查看器的过程,但这并不是真正可靠且优雅的方法. 是否还有其他用于显示图像的库,它们具有.show()和.close()之类的方法?

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

推荐答案

只需在单独的过程中打开任何图像查看器/编辑器,并在用户回答您的问题后将其杀死即可.

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

这篇关于如何使用Python显示和关闭图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆