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?
Asked
Active
Viewed 833 times
2 Answers
1
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.

Mark Ransom
- 299,747
- 42
- 398
- 622
0
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

Claudiu
- 224,032
- 165
- 485
- 680