[python-win32] get bitmapbits string from PyBITMAP pointer value

game4 sale gamz4sale at gmail.com
Sat Jun 28 08:14:37 CEST 2014


Hi guys,

I am trying to get BitmapBits from PyBITMAP object. As per the pywin32 doc,
i guess i managed to unpack the PyBITMAP structure to get the pointer to
bitmap bits. But when i use that pointer in PyGetMemory() i get
"ValueError: The value is not a valid address for reading"

Any clues please? How to get those bitmapbits as string from the pointer
value?

Please find my attached code below. I used print screen/Copy image before
running my code.

PS: I used win32ui API GetBitmapBits() successfully, but when i package my
application using py2exe it causes problems loading win32ui.pyd even after
adding MFC related DLLs. So now i am trying to avoid win32ui APIs in my
code (if possible) since i used win32ui only for BMP image handling
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20140628/1862e780/attachment.html>
-------------- next part --------------
from win32api import *
from win32clipboard import *
from win32gui import *
import struct

def get_clipboard():
    try:
        OpenClipboard()
    except:
        print "open clipboard failed"
    else:   
        if(IsClipboardFormatAvailable(CF_BITMAP)):
            print "CF_BITMAP content"
            hbitmap = GetClipboardDataHandle(CF_BITMAP)
            bminfo = GetObject(hbitmap)
            print "bmTy,bmW,bmH,bmWB,bmCP=",bminfo.bmType, bminfo.bmWidth, bminfo.bmHeight, bminfo.bmWidthBytes, bminfo.bmPlanes
            bminfo_list = []
            bminfo_list.append(bminfo.bmType)
            bminfo_list.append(bminfo.bmWidth)
            bminfo_list.append(bminfo.bmHeight)
            bminfo_list.append(bminfo.bmWidthBytes)
            bminfo_list.append(bminfo.bmPlanes)
            addr = id(bminfo)
            fmt="llllll"
            bminfo_buf = PyGetMemory(addr, struct.calcsize(fmt))
            unpacked_list = struct.unpack_from(fmt, bminfo_buf)
            print unpacked_list
            for item in unpacked_list:
                if item not in bminfo_list:
                    bits_addr = item
                    break
            print 'bits_addr =',bits_addr
            bpp = bminfo.bmWidthBytes/bminfo.bmWidth
            tot_bytes = bminfo.bmWidth * bminfo.bmHeight * bpp
            bits_str = PyGetMemory(bits_addr, tot_bytes)
            print 'len of pystr =',len(bits_str)     
        else:
            print "unsupported clipboard format"
    finally:
            CloseClipboard()

if __name__ == '__main__':
    get_clipboard()


More information about the python-win32 mailing list