PIL getpalette()返回无

我用Matlab写了一个.png图像。当我看到size(imageName)的大小时,得到480 720 3。现在,我需要在Python中获取它的调色板。我尝试了以下方法:

from PIL import Image
Image.open('path\\to\\image.png').getpalette()

当我打印输出时,得到None。我使用imwrite在Matlab中创建了图像。怎么了我应该以其他方式在Matlab中创建.png吗?有人可以帮忙吗?

yudian1988 回答:PIL getpalette()返回无

您正在使用不需要调色板的“ RGB”图像,因此getpalette()返回None。

了解更多:https://pillow.readthedocs.io/en/4.1.x/reference/Image.html

关于索引图像(带有调色板):http://www.drububu.com/tutorial/image-types.html

示例代码:

from PIL import Image
im = Image.open('image.png')
print(im.getpalette())
im2 = im.convert("P",palette=Image.ADAPTIVE,colors=256)
print(im2.getpalette())
im2.save('test.png')

输出:

None
[254,254,248,246,241,245,243,240,221,244,220,180,237,233,235,217,224,226,223,164,190,142,214,201,210,188,192,196,198,128,181,193,148,195,194,179,178,199,173,172,177,189,149,175,171,167,170,191,168,185,169,166,163,123,229,133,139,209,129,143,138,92,89,40,222,135,48,174,147,151,176,110,75,132,162,160,130,157,159,161,121,90,156,126,109,127,207,150,165,144,154,155,120,153,152,146,145,112,131,68,203,134,202,81,83,136,113,124,125,105,140,141,137,122,158,119,107,116,95,117,104,118,111,114,115,101,108,106,86,103,85,102,77,100,80,88,74,53,69,96,99,97,98,93,91,183,84,70,87,94,82,76,65,206,32,34,66,49,29,63,72,64,73,71,67,62,61,57,60,51,56,46,43,47,30,50,38,23,24,3,8,2,0]
本文链接:https://www.f2er.com/3164440.html

大家都在问