Skip to content

Instantly share code, notes, and snippets.

@urbushey
Created January 30, 2012 03:59
Show Gist options
  • Save urbushey/1702408 to your computer and use it in GitHub Desktop.
Save urbushey/1702408 to your computer and use it in GitHub Desktop.
Script to copy an image to clipboard
import win32api, sys
import Image
import win32clipboard
from cStringIO import StringIO
def send_to_clipboard(clip_type, data):
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(clip_type, data)
win32clipboard.CloseClipboard()
if __name__ == '__main__':
# get the file and verify PIL can open it
try:
image = Image.open(sys.argv[1])
except IOError, e:
win32api.MessageBox(0, 'Cannot open %s: %s' %
(sys.argv[1], str(e)),
'Copy Image to Clipboard')
output = StringIO()
# convert the file
try:
image.convert("RGB").save(output, "BMP")
except Exception, e:
win32api.MessageBox(0, 'Cannot convert %s to Bitmap: %s' %
(sys.argv[1], str(e)),
'Copy Image to Clipboard')
data = output.getvalue()[14:]
output.close()
send_to_clipboard(win32clipboard.CF_DIB, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment