2011-01-05 79 views
0

我基本上在做this但是用於8位。我可以使用「P」作爲模式位正確獲取位圖位。但是,我擁有所有這些位圖位,但沒有調色板 - PIL只使用默認的灰度調色板。如何從圖像中獲取正確的調色板?win32api:獲取位圖調色板

回答

0

這工作,返回PIL兼容的調色板:

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 
1

我不知道如何將Windows API調用轉換爲Python,也不知道如何更新PIL中的調色板,但是這裏就是了。

Windows位圖沒有連接到它們的調色板。調色板被選入DC並與保留的系統顏色合併;然後使用當前選定的調色板顯示位圖。

如果你有DC,你可以使用GetSystemPaletteEntries獲得當前實現的調色板。