我有几张图片,我想用 Python 向用户展示。用户应该输入一些描述,然后应该显示下一张图片。
这是我的代码:
#!/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 关闭图像吗?
我不需要 PIL。如果您对另一个库/bash 程序(带有子进程)有其他想法,那也很好。
原文由 Martin Thoma 发布,翻译遵循 CC BY-SA 4.0 许可协议
show
方法“主要用于调试目的”并生成一个您没有句柄的外部进程,因此您无法以正确的方式杀死它。对于 PIL,您可能希望使用其 GUI 模块之一,例如
ImageTk
、ImageQt
或ImageWin
否则,只需使用
subprocess
模块从 Python 手动生成图像查看器: