Copy Pillow image to GIMP through X11 clipboard Copy Pillow image to GIMP through X11 clipboard tkinter tkinter

Copy Pillow image to GIMP through X11 clipboard


You can send an image to an open GIMP process by using its "remote" feature. If GIMP is already running, the command gimp path/to/file.png finds GIMP's window and drops the image into the running process. This means you can create a folder with tempfile.TemporaryDirectory, save the image into this folder, and open it in GIMP.

tempfile.TemporaryDirectory is new in Python 3.2 and not available in Python 2. tempfile.NamedTemporaryFile is available in older versions of Python, but the tempfile module docs state that on UNIX, the filename can be passed to another program, but on Windows, the temporary file cannot be opened by other programs while it is open in the program that created it, and once the program that created it closes it, it will already have been deleted. Windows users with both Python 2 and Python 3 installed will need to use Python 3.3 or later in order to let the shebang line processor (PEP 397) select the appropriate version of Python.

Or if you don't want to depend on Python 3.3 or later, you could have your program detect whether it's running on Windows or POSIX and then make the appropriate action available. Under Windows, it would copy the image to the clipboard, and under POSIX, it would write the image to a tempfile.NamedTemporaryFile, pass the filename to gimp or whatever other program the user specifies to receive it, and then destroy the temporary file once a new one is created or the application closes.