Skip to content

Instantly share code, notes, and snippets.

@urbushey
Created January 30, 2012 03:59
Show Gist options
  • Select an option

  • Save urbushey/1702408 to your computer and use it in GitHub Desktop.

Select an option

Save urbushey/1702408 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Jan 30, 2012.
    32 changes: 32 additions & 0 deletions copytoclipboard.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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)