开发者

win32api: get bitmap palette

I'm basically doing this but for 8-bit. I can get the bitmap bits correctly using "P" as the mode bit. However, I have all these bitmap bits, but no palette - PIL just uses a default gray-scale palette. 开发者_StackOverflowHow do I get the correct palette from the image?


I'm not sure how to convert Windows API calls into Python, nor do I know how to update a palette in PIL, but here goes.

Windows bitmaps don't have a color palette attached to them. The palette is selected into the DC and merged with the reserved system colors; the bitmap is then displayed using the currently selected palette.

If you have the DC you can get the currently realized palette using GetSystemPaletteEntries.


This works, returning a PIL-compatible palette:

import ctypes, win32gui
def getSystemPalette():
    hwnd = win32gui.GetDesktopWindow()

    hwndDC = win32gui.GetWindowDC(hwnd)

    buff = ctypes.c_buffer("0"*(256*4)) #R, G, B, and flags
    ctypes.windll.gdi32.GetSystemPaletteEntries(hwndDC, 0, 256, buff)

    win32gui.ReleaseDC(hwnd, hwndDC)

    #ignore every 4th entry which is the flags
    res = [ord(x) for i,x in enumerate(buff) if i%4 != 3]
    return res
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜