PIL getpalette()返回无

时间:2019-11-04 18:40:24

标签: python matlab python-imaging-library

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

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

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

1 个答案:

答案 0 :(得分:1)

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