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.
This is my code:
#!/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
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?
I don't need PIL. If you have another idea with another library / bash-program (with subprocess), it'll be also fine.
You can also use the keyboard shortcut, Ctrl+W (Win) / Command+W (Mac): To close a single image, go to File > Close. Another way to close a single image is by clicking the small "x" icon in the document's tab.
Python – Display Image using PIL To show or display an image in Python Pillow, you can use show() method on an image object. The show() method writes the image to a temporary file and then triggers the default program to display that image. Once the program execution is completed, the temporary file will be deleted.
psutil can get the pid of the display
process created by im.show()
and kill the process with that pid on every operating system:
import time
import psutil
from PIL import Image
# open and show image
im = Image.open('myImageFile.jpg')
im.show()
# display image for 10 seconds
time.sleep(10)
# hide image
for proc in psutil.process_iter():
if proc.name() == "display":
proc.kill()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With