How do I read a jpg or png from the windows clipboard in python and vice versa?

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.

Asked By: foosion
||

Answer #1:

I would just use Pillow:

from PIL import ImageGrab
im = ImageGrab.grabclipboard()
im.save('somefile.png','PNG')
Answered By: foosion

Answer #2:

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.

Answered By: Gerrat

Answer #3:

Using PythonMagick (binaries):

from PythonMagick import Image
Image("clipboard:").write("PNG32:clipboard.png")  # clipboard -> file
Image("clipboard.png").write("clipboard:")  # file -> clipboard
Answered By: Mark Ransom

Answer #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.

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.
Answered By: cgohlke
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .



# More Articles