简体   繁体   English

通过 X11 剪贴板将枕头图像复制到 GIMP

[英]Copy Pillow image to GIMP through X11 clipboard

I'm using Xubuntu 14.04 (an X11/Linux distribution).我使用的是 Xubuntu 14.04(X11/Linux 发行版)。 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.我已经使用 Pillow(Python 图像库)和 Tkinter 库编写了一个 Python 程序,以使用自定义位图字体将用户输入的文本呈现为图像。 I want to quickly bring this image into the running GIMP process as a new layer.我想快速将这个图像作为一个新层带入正在运行的 GIMP 进程中。 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.目前它的工作方式是将图像保存为 PNG 文件,然后要求用户使用 GIMP 的文件选择器去挖掘 PNG 文件。 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.我认为如果我可以将图像保存到 X11 剪贴板以便我可以将其粘贴到 GIMP 中会更方便。

But all the examples for interacting with the clipboard in Tkinter that I could find through Google are for text, not images.但是我可以通过 Google 找到的 与 Tkinter剪贴板交互的所有 示例都是针对文本的,而不是针对图像的。 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 .我找到了如何在 Windows ( 1 , 2 ) 中复制图像:将 BMP 文件保存到BytesIO ,切掉前 14 个字节,并将其余的作为CF_DIB发送到 Windows 剪贴板。 But I don't want to require Wine.但我不想需要Wine。 There also exists an answer about how to do it in GTK+ ;还有一个关于如何在 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?是否值得从移植到Tkinter的在PyGTK的应用程序(GTK + 2)或PyGObject(为GTK + 3),尽管安装的头痛,这可能会导致Windows用户? Or is there an easier way than the X11 clipboard to get a PIL.Image.Image instance into GIMP?或者是否有比 X11 剪贴板更简单的方法将PIL.Image.Image实例放入 GIMP?

You can send an image to an open GIMP process by using its "remote" feature.您可以使用其“远程”功能将图像发送到打开的 GIMP 进程。 If GIMP is already running, the command gimp path/to/file.png finds GIMP's window and drops the image into the running process .如果 GIMP 已经在运行,命令gimp path/to/file.png找到 GIMP 的窗口并将图像放入正在运行的进程中 This means you can create a folder with tempfile.TemporaryDirectory , save the image into this folder, and open it in GIMP.这意味着您可以使用tempfile.TemporaryDirectory创建一个文件夹,将图像保存到该文件夹​​中,然后在 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. tempfile.TemporaryDirectory 在 Python 3.2 中新的,在 Python 2 中不可用。 tempfile.NamedTemporaryFile在旧版本的 Python 中可用,但tempfile 模块文档指出,在 UNIX 上,文件名可以传递给另一个程序,但在 Windows 上,临时文件在创建它的程序中打开时不能被其他程序打开,一旦创建它的程序关闭它,它就已经被删除了。 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.安装了 Python 2 和 Python 3 的 Windows 用户将需要使用 Python 3.3 或更高版本,以便让 shebang 行处理器 ( PEP 397 ) 选择合适的 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.或者,如果您不想依赖 Python 3.3 或更高版本,您可以让您的程序检测它是在 Windows 还是 POSIX 上运行,然后使适当的操作可用。 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.在 Windows 下,它将图像复制到剪贴板,在 POSIX 下,它将图像写入tempfile.NamedTemporaryFile ,将文件名传递给gimp或用户指定的任何其他程序来接收它,然后销毁临时文件一次创建一个新的或应用程序关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM