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.
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.
I would just use Pillow:
from PIL import ImageGrab
im = ImageGrab.grabclipboard()
im.save('somefile.png','PNG')
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.
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.
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:
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.
Using PythonMagick (binaries):
from PythonMagick import Image
Image("clipboard:").write("PNG32:clipboard.png") # clipboard -> file
Image("clipboard.png").write("clipboard:") # file -> clipboard
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.
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:
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.