Close Image with subprocess

2.2k views Asked by At

I am trying to open an image with subprocess so it is visible to the user, then close the image so that it disapears.

This question has been asked before, but the answers I found have not worked for me. Here is what I have checked:

Killing a process created with Python's subprocess.Popen()

How to terminate a python subprocess launched with shell=True

How can I close an image shown to the user with the Python Imaging Library?

I need the code to open an image (with Preview (optional), default on my Mac), wait a second, then close the image.

openimg = subprocess.Popen(["open", mypath])
time.sleep(1)
openimg.terminate()
openimg.kill()

Everything is telling me to terminate() and kill(), but that still isn't closing the image.

This does not HAVE to use preview, I am open to hearing other options as well.

Edit: I have additionally tried the below code, still no success

print('openimg\'s pid = ',openimg.pid)
os.kill(openimg.pid, signal.SIGKILL)
2

There are 2 answers

0
Drake491 On BEST ANSWER

I'm sure this is the hackiest way to do this, but hell, it works. If anyone stumbles on this and knows a better way, please let me know.

The Problem As David Z described, I was not targeting the correct process. So I had to figure out what the correct process ID was in order to close it properly.

I started by opening my terminal and entering the command ps -A. This will show all of the current open processes and their ID. Then I just searched for Preview and found two open processes.

Previously, I was closing the first Preview pid in the list, let's call it 11329 but the second on the list was still open. The second Preview process, 11330, was always 1 digit higher then the first process. This second one was the pid I needed to target to close Preview.

The Answer

openimg = subprocess.Popen(["open", mypath]) ##Opens the image to view
newpid = openimg.pid + 1 ##Gets the pid 1 digit higher than the openimg pid. 
os.kill(newpid, signal.SIGKILL) ##Kills the pid and closes Preview

This answer seems very fragile, but it works for me. I only just started learning about pids so if anyone could provide some knowledge, I would be grateful.

0
David Z On

OS X's open command is asynchronous - it spawns a Preview process (or whatever app it launches) and immediately exits. So by the time your Python code gets around to calling terminate() and kill(), the open process is done. It no longer exists.

You can force synchronous behavior, i.e. make open keep running until after Preview exits, by passing the -W option,

subprocess.Popen(["open", "-W", mypath])

This way, open will still be running when your code gets around to running terminate and kill. (I would suggest also passing the -n option to make sure Preview starts a new instance, in case you had another instance of Preview sitting around from before.) And when the open process ends, hopefully it will also end the underlying Preview process. You can check whether this actually happens using a process viewer such as ps or pgrep.

If terminating or killing open does not kill Preview, you'll need to either change the configuration so that the signal is delivered to all subprocesses of open when you call terminate() or kill(), for which this question or this one may be helpful, or you'll have to find a way to get the process ID of Preview and send signals to that directly, which will require you to go beyond Popen. I'm not sure of the best way to do that, but perhaps someone else can contribute an answer that shows you how.