如何关闭使用Python Imaging Library向用户显示的图像? [英] How can I close an image shown to the user with the Python Imaging Library?

查看:81
本文介绍了如何关闭使用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

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

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