Copying a PIL image to windows clipboard?

Discussion in 'Python' started by Gey-Hong Gweon, Aug 22, 2003.

  1. Is there a way to copy a PIL image to windows clipboard (as a dib
    image or a bitmap, I suppose)?

    What I would like to accomplish is to do a fast copy and paste of
    images from my python application to a windows application such as
    powerpoint. A slower way is to use files, which is what I am doing at
    the moment.

    Thanks in advance.
     
    Gey-Hong Gweon, Aug 22, 2003
    #1
    1. Advertising

  2. Hi !

    I have pb with...

    My code :

    import Image,ImageGrab
    buffer = ImageGrab.grab()


    And result :
    Traceback (most recent call last):
    File "D:\dev\python\ess.py", line 1, in ?
    import Image,ImageGrab
    File "C:\PYTHON22\Lib\site-packages\PIL\ImageGrab.py", line 32, in
    ?
    import _grabscreen
    ImportError: No module named _grabscreen


    Other PIL's fonction run OK. Oups ! I remember : Python 2.2 & PIL 1.1.4


    An idea ? Thanks. And sorry for my poor english.

    @+
    --
    Michel Claveau
     
    News M Claveau /Hamster-P, Aug 27, 2003
    #2
    1. Advertising

  3. "News M Claveau /Hamster-P" <> schrieb im Newsbeitrag
    news:bij45g$njv$...

    Maybe you should have a look into win32 which will give you full controll of
    the clipboard: Here is the example code from Mark Hammond's demos. Of course
    you have to find out a format which is understood by the programms you want
    to paste the images in. But PIL is very good in converting formats ;-)

    Kindly
    Michael P


    # win32clipboardDemo.py
    #
    # Demo/test of the win32clipboard module.
    from win32clipboard import *
    import win32con
    import types

    if not __debug__:
    print "WARNING: The test code in this module uses assert"
    print "This instance of Python has asserts disabled, so many tests will
    be skipped"

    cf_names = {}
    # Build map of CF_* constants to names.
    for name, val in win32con.__dict__.items():
    if name[:3]=="CF_" and name != "CF_SCREENFONTS": #
    CF_SCREEN_FONTS==CF_TEXT!?!?
    cf_names[val] = name

    def TestEmptyClipboard():
    OpenClipboard()
    try:
    EmptyClipboard()
    assert EnumClipboardFormats(0)==0, "Clipboard formats were available
    after emptying it!"
    finally:
    CloseClipboard()

    def TestText():
    OpenClipboard()
    try:
    text = "Hello from Python"
    SetClipboardText(text)
    got = GetClipboardData(win32con.CF_TEXT)
    assert got == text, "Didnt get the correct result back - '%r'." %
    (got,)
    # Win32 documentation says I can get the result back as CF_UNICODE
    or CF_OEMTEXT.
    # But it appears I need to close the clipboard for this to kick-in.
    # but if I attempt to, it fails!
    finally:
    CloseClipboard()

    OpenClipboard()
    try:
    got = GetClipboardData(win32con.CF_UNICODETEXT)
    assert got == text, "Didnt get the correct result back - '%r'." %
    (got,)
    assert type(got)==types.UnicodeType, "Didnt get the correct result
    back - '%r'." % (got,)

    got = GetClipboardData(win32con.CF_OEMTEXT)
    assert got == text, "Didnt get the correct result back - '%r'." %
    (got,)

    # Unicode tests
    EmptyClipboard()
    text = u"Hello from Python unicode"
    # Now set the Unicode value
    SetClipboardData(win32con.CF_UNICODETEXT, text)
    # Get it in Unicode.
    got = GetClipboardData(win32con.CF_UNICODETEXT)
    assert got == text, "Didnt get the correct result back - '%r'." %
    (got,)
    assert type(got)==types.UnicodeType, "Didnt get the correct result
    back - '%r'." % (got,)

    # Close and open the clipboard to ensure auto-conversions take
    place.
    finally:
    CloseClipboard()

    OpenClipboard()
    try:

    # Make sure I can still get the text.
    got = GetClipboardData(win32con.CF_TEXT)
    assert got == text, "Didnt get the correct result back - '%r'." %
    (got,)
    # Make sure we get back the correct types.
    got = GetClipboardData(win32con.CF_UNICODETEXT)
    assert type(got)==types.UnicodeType, "Didnt get the correct result
    back - '%r'." % (got,)
    got = GetClipboardData(win32con.CF_OEMTEXT)
    assert got == text, "Didnt get the correct result back - '%r'." %
    (got,)
    print "Clipboard text tests worked correctly"
    finally:
    CloseClipboard()

    def TestClipboardEnum():
    OpenClipboard()
    try:
    # Enumerate over the clipboard types
    enum = 0
    while 1:
    enum = EnumClipboardFormats(enum)
    if enum==0:
    break
    assert IsClipboardFormatAvailable(enum), "Have format, but
    clipboard says it is not available!"
    n = cf_names.get(enum,"")
    if not n:
    try:
    n = GetClipboardFormatName(enum)
    except error:
    n = "unknown (%s)" % (enum,)

    print "Have format", n
    print "Clipboard enumerator tests worked correctly"
    finally:
    CloseClipboard()

    class Foo:
    def __init__(self, **kw):
    self.__dict__.update(kw)
    def __cmp__(self, other):
    return cmp(self.__dict__, other.__dict__)

    def TestCustomFormat():
    OpenClipboard()
    try:
    # Just for the fun of it pickle Python objects through the clipboard
    fmt = RegisterClipboardFormat("Python Pickle Format")
    import cPickle
    pickled_object = Foo(a=1, b=2, Hi=3)
    SetClipboardData(fmt, cPickle.dumps( pickled_object ) )
    # Now read it back.
    data = GetClipboardData(fmt)
    loaded_object = cPickle.loads(data)
    assert cPickle.loads(data) == pickled_object, "Didnt get the correct
    data!"

    print "Clipboard custom format tests worked correctly"
    finally:
    CloseClipboard()


    if __name__=='__main__':
    TestEmptyClipboard()
    TestText()
    TestCustomFormat()
    TestClipboardEnum()
    # And leave it empty at the end!
    TestEmptyClipboard()
     
    Michael Peuser, Aug 29, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    802
  2. Dan Bishop
    Replies:
    0
    Views:
    270
    Dan Bishop
    Aug 26, 2008
  3. Replies:
    4
    Views:
    310
    Randy Webb
    Dec 14, 2004
  4. Mahsha

    Clipboard - Copy Image To Clipboard

    Mahsha, Jun 1, 2009, in forum: Javascript
    Replies:
    1
    Views:
    651
    Thomas 'PointedEars' Lahn
    Jun 1, 2009
  5. Mahsha

    Clipboard - Copy Text To Clipboard

    Mahsha, Jun 1, 2009, in forum: Javascript
    Replies:
    3
    Views:
    698
    Thomas 'PointedEars' Lahn
    Jun 1, 2009
Loading...

Share This Page