2

I'm using Xubuntu 14.04 (an X11/Linux distribution). I've written a Python program using the Pillow (Python Imaging Library) and Tkinter libraries to render user-entered text into an image using a custom bitmap font. I want to quickly bring this image into the running GIMP process as a new layer. Currently it operates by saving the image to a PNG file and then requiring the user to go dig for the PNG file using GIMP's file picker. I thought it would be more convenient if I could save the image to the X11 clipboard so that I could paste it into GIMP.

But all the examples for interacting with the clipboard in Tkinter that I could find through Google are for text, not images. I found how to copy an image in Windows (1, 2): save a BMP file to a BytesIO, chop off the first 14 bytes, and send the rest to the Windows clipboard as a CF_DIB. But I don't want to require Wine. There also exists an answer about how to do it in GTK+; is it worth porting the application from Tkinter to PyGTK (for GTK+ 2) or PyGObject (for GTK+ 3), despite the installation headache that it may cause for Windows users? Or is there an easier way than the X11 clipboard to get a PIL.Image.Image instance into GIMP?

Community
  • 1
  • 1
Damian Yerrick
  • 4,602
  • 2
  • 26
  • 64
  • It looks like putting images to the clipboard in Tk, and thus TkInter, is not well-supported. You can *get* image data easily with `clipboard get -type ` (the list of types can be obtained with `clipboard get -type TARGETS`; image/png works with selections created by gimp). But try putting the same data back in, and gimp can't recognize it as an image. Perhaps some digging into Tk sources will help. – n. m. could be an AI Dec 24 '15 at 12:38

1 Answers1

1

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.

Community
  • 1
  • 1
Damian Yerrick
  • 4,602
  • 2
  • 26
  • 64