Search code examples
pythonpycharmpyautogui

pyautogui GIMP automation problems


I'm doing some automation with GIMP as my test case. I need to launch the program and send mouse/keyboard actions to it. I'm using subprocess.call() to start the program and pyautogui to send mouse/keyboard actions.

import pyautogui
import subprocess

subprocess.call('gimp', shell=True)    
gimp_file = pyautogui.locateOnScreen('imgs/gimp_file.png', grayscale=True,region=(5, 28, 29, 19))

file_button_x, file_button_y = pyautogui.center(gimp_file)  
pyautogui.click(file_button_x, file_button_y, duration=0.75)

The issue I'm having is that the pyautogui part of code is never hit, the program appears to just hang. I've run trough the debugger in PyCharm and no variables are returned. If I run subprocess.call('gimp') in python console and the rest on the code in another console it does what it should. I think can be either time delay caused by program start or window focus; I've adding time.sleep(5) to address the former cause, but no affect. How I can address this?


Solution

  • subprocess.call() will wait for the process to exit - which won't happen (unless you quit GIMP), nor do you want it to wait. You should use subprocess.Popen(), with stdin set to subprocess.DEVNULL and stdout/stderr either left alone or also redirected to DEVNULL. This will allow your program to proceed to the pyautogui lines, while GIMP is running.

    NOTE that gimp displays a splash screen, so you might need to wait a bit before you attempt to find its window and 'click' in it.