2010-07-06 192 views
1

從給定的.png文件生成8位調色板的最佳基於python的庫是什麼? 在以.pal格式生成的photoshop中。通過Python從png文件生成8位調色板

PS:輸入PNG已經是8位格式。 (調色板)

問候

+0

png文件是否已調色?或者它是真實的顏色? – slurdge 2010-07-06 09:02:22

+0

我已編輯澄清問題。 – Hellnar 2010-07-06 09:08:09

+0

那根本不是「生成」的;這只是「提取」。 – Nyerguds 2017-03-31 11:16:19

回答

2

我一直無法找到.PAL的規格(Photoshop稱之爲「Microsoft PAL」),但格式很容易反向工程。這工作:

def extractPalette(infile,outfile): 
    im=Image.open(infile) 
    pal=im.palette.palette 
    if im.palette.rawmode!='RGB': 
     raise ValueError("Invalid mode in PNG palette") 
    output=open(outfile,'wb') 
    output.write('RIFF\x10\x04\x00\x00PAL data\x04\x04\x00\x00\x00\x03\x00\x01') # header 
    output.write(''.join(pal[i:i+3]+'\0' for i in range(0,768,3))) # convert RGB to RGB0 before writing 
    output.close() 
+0

作品像一個魅力,謝謝foone! – Hellnar 2010-07-08 03:30:56

1

如果它是一個palletted圖像,然後你可以使用getcolors()方法,一旦你已經裝載入PIL。如果是RGB或RGBA圖像,則需要進行色彩還原,直到最多有256色。

+0

感謝您的建議Ignacio,那麼如何生成此方法的.pal調色板版本? Regards – Hellnar 2010-07-06 09:19:16

+0

我不知道該文件格式是什麼樣子。我懷疑你最終需要使用'struct'。 – 2010-07-06 09:34:19