问题
I have some Python code that edits image files and would like to know how
to add image data to the Operating System clipboard and get it from there.
When searching for a cross-platform solution to replace or get clipboard text in Python,
there were many simple answers to do it (e.g. using the built-in Tkinter module with some code).
However, these methods could only use plain text, not other clipboard data like images.
My version of Python is 3.x on Windows but the answer needs to be cross-platform
(work on different operating systems) and should also support other Python versions like 2.x.
I think it should only use built-in Python modules and the code shouldn't be too complex (or have an explanation of what it does). It can be a Python module because the files can be included in the same folder as portable program code to avoid installing.
There are some other related questions to this one that probably work for images but they only support an individual operating system. The best were Copy image to clipboard in Python3 and Write image to Windows clipboard in python with PIL and win32clipboard?.
The methods described there (for Windows only) appear to use the following steps:
- Get raw binary data of the image - the method loads an image file with the Python Imaging
Library (PIL/Pillow) module because this has other processing features used later, in a simple
and popular standard API. This could be done with a different module instead (e.g. Pygame). - Create a file object variable (for in-memory input/output streaming), using the built-in io
module. For Python 2.x,from cStringIO import StringIO
is used but with Python 3, the
betterio.BytesIO
binary stream object type is used - the older ones now only allow text. - Save the image data, in the BMP (Windows Bitmap/Device Independent Bitmap) file format,
to the file object variable from the previous step. The method using PIL/Pillow first converts
this data with.convert("RGB")
on the variable containing it. - Get the full contents of the file object variable memory buffer as binary data (
bytes
object),
slice it from position 14 to remove the 14-byte header of the BMP/DIB file format, then save it
as a variable. The method says the slicing of this data works on 32 or 64 bit systems but
needs the Windows clipboard API so doesn't work for a different file format. - Close the memory buffer and copy the image data from the previous step to the clipboard.
The method does this on Windows using thewin32clipboard
part of an extension module -
it opens the clipboard for use, clears it, sets its value to the image data variable from the
previous step (using the BMP/DIB type) then closes the open clipboard.
Also, there's a simple cross-platform clipboard text module called Pyperclip, which is
only a single file for version 1.5.6 and maybe has code that could process image data.
回答1:
I don't think you could interact with the clipboard without external module.
Clipboard APIs are different from different Operating systems.
I suggest you to use the clipboard module.
https://pypi.python.org/pypi/clipboard/0.0.4
回答2:
The xerox
library is simple and capable.
回答3:
Here's a Python function based on this answer that replaces/returns clipboard text using Tkinter.
def use_clipboard(paste_text=None):
import tkinter # For Python 2, replace with "import Tkinter as tkinter".
tk = tkinter.Tk()
tk.withdraw()
if type(paste_text) == str: # Set clipboard text.
tk.clipboard_clear()
tk.clipboard_append(paste_text)
try:
clipboard_text = tk.clipboard_get()
except tkinter.TclError:
clipboard_text = ''
r.update() # Stops a few errors (clipboard text unchanged, command line program unresponsive, window not destroyed).
tk.destroy()
return clipboard_text
This method creates a quickly hidden window which is closed quickly so
that shouldn't be a problem. Also, it only supports using plain text in the
clipboard, not images which I asked for in the question above.
回答4:
As stated by Martin---
Pyperclip is definitely a good option and works like a charm.
I don't know why you shouldn't be using it.
Its as simple as 3 lines below,
import pyperclip
pyperclip.copy('The text to be copied to the clipboard.')
paste= pyperclip.paste()
来源:https://stackoverflow.com/questions/24474536/how-to-add-get-image-data-in-the-os-clipboard-using-python