I have an image (jpg, png, etc.) in the windows clipboard. I'd like to save it to a file. win32clipboard would seem to be the answer, but every example I can find deals with text.
我在windows剪贴板上有一个图片(jpg、png等)。我想把它保存到一个文件中。win32clipboard似乎是答案,但我能找到的每个例子都与文本有关。
copy an image to the clipboard, then
然后将图像复制到剪贴板
import win32clipboard
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
with open(name, 'wb') as f:
f.write(data)
win32clipboard.CloseClipboard()
fails with
失败与
TypeError: Specified clipboard format is not available
I'd also like to do the reverse - given an image file, write it to the clipboard.
我还想做反向-给定一个图像文件,把它写到剪贴板。
4 个解决方案
#1
18
I would just use Pillow:
我就用枕头:
from PIL import ImageGrab
im = ImageGrab.grabclipboard()
im.save('somefile.png','PNG')
#2
4
The function win32clipboard.GetClipboardData()
has a parameter. The default parameter specifies that you want the contents of the clipboard as text. You need to pass in the value that specifies the data format you want the clipboard to give you.
函数win32clipboard.GetClipboardData()有一个参数。默认参数指定要将剪贴板的内容作为文本。您需要传递指定要剪贴板给您的数据格式的值。
The standard clipboard data formats are documented here.
这里记录了标准的剪贴板数据格式。
ALSO:
另外:
See here for documentation on EnumClipboardFormats()
-- basically, you need code like this (untested) to get the formats that are available currently on the clipboard:
在这里查看有关EnumClipboardFormats()的文档——基本上,您需要这样的代码(未经测试)来获取剪贴板上当前可用的格式:
formats = []
lastFormat = 0
while 1:
nextFormat = win32clipboard.EnumClipboardFormats(lastFormat)
if 0 == nextFormat:
# all done -- get out of the loop
break
else:
formats.append(nextFormat)
lastFormat = nextFormat
# when you get here, formats contains a list of format codes that
# you can retrieve from the clipboard right now.
#3
4
You need to pass a parameter to GetClipboardData
specifying the format of the data you're looking for. You can use EnumClipboardFormats
to see the formats that are available - when I copy something in Explorer there are 15 formats available to me.
您需要向GetClipboardData传递一个参数,指定要查找的数据的格式。您可以使用EnumClipboardFormats来查看可用的格式—当我在Explorer中复制某样东西时,我可以使用15种格式。
Edit 2: Here's the code to get a filename after you've copied a file in Explorer. The answer will be completely different if you've copied an image from within a program, a browser for example.
编辑2:这是在浏览器中复制文件后获取文件名的代码。如果你从一个程序(例如浏览器)中复制了一张图片,答案就完全不同了。
import win32clipboard
win32clipboard.OpenClipboard()
filename_format = win32clipboard.RegisterClipboardFormat('FileName')
if win32clipboard.IsClipboardFormatAvailable(filename_format):
input_filename = win32clipboard.GetClipboardData(filename_format)
win32clipboard.CloseClipboard()
Edit 3: From the comments it's clear you have an actual image in the clipboard, not the filename of an image file. You've stated that you can't use PIL, so:
编辑3:从注释中可以看出,剪贴板中有一个真实的图像,而不是图像文件的文件名。你说过你不能使用PIL,所以:
import win32clipboard
win32clipboard.OpenClipboard()
if win32clipboard.IsClipboardFormatAvailable(win32clipboard.CF_DIB):
data = win32clipboard.GetClipboardData(win32clipboard.CF_DIB)
win32clipboard.CloseClipboard()
At this point you have a string (in Python 2) or bytes (in Python 3) that contains the image data. The only format you'll be able to save is .BMP, and you'll have to decode the BITMAPINFOHEADER to get the parameters for a BITMAPFILEHEADER that needs to be written to the front of the file.
此时,您有一个包含图像数据的字符串(在Python 2中)或字节(在Python 3中)。您可以保存的唯一格式是. bmp,并且您必须解码BITMAPINFOHEADER来获取位mapfileheader的参数,这些参数需要写到文件的前面。
#4
2
Using PythonMagick (binaries):
使用PythonMagick(二进制文件):
from PythonMagick import Image
Image("clipboard:").write("PNG32:clipboard.png") # clipboard -> file
Image("clipboard.png").write("clipboard:") # file -> clipboard
#1
18
I would just use Pillow:
我就用枕头:
from PIL import ImageGrab
im = ImageGrab.grabclipboard()
im.save('somefile.png','PNG')
#2
4
The function win32clipboard.GetClipboardData()
has a parameter. The default parameter specifies that you want the contents of the clipboard as text. You need to pass in the value that specifies the data format you want the clipboard to give you.
函数win32clipboard.GetClipboardData()有一个参数。默认参数指定要将剪贴板的内容作为文本。您需要传递指定要剪贴板给您的数据格式的值。
The standard clipboard data formats are documented here.
这里记录了标准的剪贴板数据格式。
ALSO:
另外:
See here for documentation on EnumClipboardFormats()
-- basically, you need code like this (untested) to get the formats that are available currently on the clipboard:
在这里查看有关EnumClipboardFormats()的文档——基本上,您需要这样的代码(未经测试)来获取剪贴板上当前可用的格式:
formats = []
lastFormat = 0
while 1:
nextFormat = win32clipboard.EnumClipboardFormats(lastFormat)
if 0 == nextFormat:
# all done -- get out of the loop
break
else:
formats.append(nextFormat)
lastFormat = nextFormat
# when you get here, formats contains a list of format codes that
# you can retrieve from the clipboard right now.
#3
4
You need to pass a parameter to GetClipboardData
specifying the format of the data you're looking for. You can use EnumClipboardFormats
to see the formats that are available - when I copy something in Explorer there are 15 formats available to me.
您需要向GetClipboardData传递一个参数,指定要查找的数据的格式。您可以使用EnumClipboardFormats来查看可用的格式—当我在Explorer中复制某样东西时,我可以使用15种格式。
Edit 2: Here's the code to get a filename after you've copied a file in Explorer. The answer will be completely different if you've copied an image from within a program, a browser for example.
编辑2:这是在浏览器中复制文件后获取文件名的代码。如果你从一个程序(例如浏览器)中复制了一张图片,答案就完全不同了。
import win32clipboard
win32clipboard.OpenClipboard()
filename_format = win32clipboard.RegisterClipboardFormat('FileName')
if win32clipboard.IsClipboardFormatAvailable(filename_format):
input_filename = win32clipboard.GetClipboardData(filename_format)
win32clipboard.CloseClipboard()
Edit 3: From the comments it's clear you have an actual image in the clipboard, not the filename of an image file. You've stated that you can't use PIL, so:
编辑3:从注释中可以看出,剪贴板中有一个真实的图像,而不是图像文件的文件名。你说过你不能使用PIL,所以:
import win32clipboard
win32clipboard.OpenClipboard()
if win32clipboard.IsClipboardFormatAvailable(win32clipboard.CF_DIB):
data = win32clipboard.GetClipboardData(win32clipboard.CF_DIB)
win32clipboard.CloseClipboard()
At this point you have a string (in Python 2) or bytes (in Python 3) that contains the image data. The only format you'll be able to save is .BMP, and you'll have to decode the BITMAPINFOHEADER to get the parameters for a BITMAPFILEHEADER that needs to be written to the front of the file.
此时,您有一个包含图像数据的字符串(在Python 2中)或字节(在Python 3中)。您可以保存的唯一格式是. bmp,并且您必须解码BITMAPINFOHEADER来获取位mapfileheader的参数,这些参数需要写到文件的前面。
#4
2
Using PythonMagick (binaries):
使用PythonMagick(二进制文件):
from PythonMagick import Image
Image("clipboard:").write("PNG32:clipboard.png") # clipboard -> file
Image("clipboard.png").write("clipboard:") # file -> clipboard