문제

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. How do I get the correct palette from the image?

도움이 되었습니까?

해결책 2

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top