3 回答

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊
是的,這樣:
im = Image.open('image.gif')
rgb_im = im.convert('RGB')
r, g, b = rgb_im.getpixel((1, 1))
print(r, g, b)
(65, 100, 137)
之所以之前獲得單個(gè)值,pix[1, 1]是因?yàn)镚IF像素引用了GIF調(diào)色板中的256個(gè)值之一。
另請參見此 SO帖子:GIF和JPEG的Python和PIL像素值不同,并且此PIL參考頁面 包含有關(guān)該convert()函數(shù)的更多信息。
順便說一句,您的代碼將對.jpg圖像正常工作。

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊
GIF將顏色存儲為調(diào)色板中x種可能顏色中的一種。閱讀有關(guān)gif受限調(diào)色板的信息。因此,PIL為您提供調(diào)色板索引,而不是該調(diào)色板顏色的顏色信息。
編輯:刪除了具有錯(cuò)字的博客帖子解決方案的鏈接。其他答案也做同樣的事情而沒有錯(cuò)字。

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
轉(zhuǎn)換圖像的另一種方法是從調(diào)色板創(chuàng)建RGB索引。
from PIL import Image
def chunk(seq, size, groupByList=True):
"""Returns list of lists/tuples broken up by size input"""
func = tuple
if groupByList:
func = list
return [func(seq[i:i + size]) for i in range(0, len(seq), size)]
def getPaletteInRgb(img):
"""
Returns list of RGB tuples found in the image palette
:type img: Image.Image
:rtype: list[tuple]
"""
assert img.mode == 'P', "image should be palette mode"
pal = img.getpalette()
colors = chunk(pal, 3, False)
return colors
# Usage
im = Image.open("image.gif")
pal = getPalletteInRgb(im)
添加回答
舉報(bào)