如何关闭使用Python Imaging Library向用户显示的图像?
我有几幅图像,我想用Python向用户展示.用户应输入一些描述,然后应显示下一张图像.
I have several images which I would like to show the user with Python. The user should enter some description and then the next image should be shown.
这是我的代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, glob
from PIL import Image
path = '/home/moose/my/path/'
for infile in glob.glob( os.path.join(path, '*.png') ):
im = Image.open(infile)
im.show()
value = raw_input("Description: ")
# store and do some other stuff. Now the image-window should get closed
它正在工作,但是用户必须自己关闭图像.输入说明后,我可以让python关闭图片吗?
It is working, but the user has to close the image himself. Could I get python to close the image after the description has been entered?
我不需要PIL.如果您对另一个库/bash程序(带有子进程)有其他想法,也可以.
I don't need PIL. If you have another idea with another library / bash-program (with subprocess), it'll be also fine.
show
方法主要用于调试目的",并产生一个外部进程,您无法获取该进程的句柄,因此无法以适当的方式杀死它.
The show
method "is mainly intended for debugging purposes" and spawns an external process for which you don't get a handle, so you can't kill it in a proper way.
使用PIL,您可能需要使用其GUI模块之一,例如 ImageTk
, ImageQt
或 ImageWin
.
With PIL, you may want to use one of its GUI modules , such as ImageTk
, ImageQt
or ImageWin
.
否则,只需使用subprocess
模块从Python手动生成图像查看器:
Otherwise, just manually spawn an image viewer from Python with the subprocess
module:
for infile in glob.glob( os.path.join(path, '*.png')):
viewer = subprocess.Popen(['some_viewer', infile])
viewer.terminate()
viewer.kill() # make sure the viewer is gone; not needed on Windows