我有一堆分开处理的图片,处理完后需要拼接在一起。为了节省空间,我想将它们压缩成8位图像。目前我是通过调用PIL来实现的:
image = image.convert('P', palette=Image.ADAPTIVE, colors=256)
问题是每个图像的调色板都不同。我想让所有图像使用相同的调色板。
例如,这是我从其中一张图像中提取出来并进行转换和压缩后得到的调色板。
[(242, 239, 233), (233, 233, 212), (210, 209, 206), (194, 193, 189), (194, 191, 184), (158, 183, 205), (191, 189, 185), (192, 188, 182), (190, 188, 182), (191, 187, 181), (204, 175, 85), (164, 147, 83), (112, 130, 149), (190, 187, 181), (254, 249, 115), (154, 179, 204), (255, 250, 115), (241, 238, 232), (167, 204, 149), (251, 246, 115), (250, 245, 115), (241, 190, 35), (208, 199, 126), (249, 243, 115), (155, 180, 204), (246, 241, 116), (168, 204, 151), (253, 248, 115), (236, 229, 118), (156, 182, 197), (171, 199, 154), (163, 197, 163), (231, 224, 119), (153, 179, 202), (225, 217, 122), (252, 247, 115), (204, 201, 195), (170, 205, 152), (215, 212, 206), (206, 214, 220), (213, 210, 204), (196, 208, 217), (215, 217, 199), (210, 207, 200), (219, 211, 128), (214, 206, 130), (203, 220, 190), (192, 215, 177), (207, 204, 198), (178, 208, 161), (167, 204, 150), (205, 201, 190), (241, 235, 117), (198, 195, 189), (197, 194, 188), (232, 232, 229), (195, 192, 186), (194, 191, 185), (193, 190, 184), (196, 186, 157), (242, 191, 36), (240, 238, 232), (240, 238, 231), (205, 189, 132), (183, 199, 213), (167, 188, 208), (164, 186, 207), (161, 184, 206), (242, 236, 115), (156, 181, 205), (156, 181, 204), (232, 229, 221), (192, 189, 183), (191, 188, 182), (231, 228, 215), (190, 188, 181), (240, 237, 231), (239, 236, 230), (222, 219, 212), (238, 236, 231), (189, 185, 167), (178, 185, 168), (219, 226, 215), (219, 223, 225), (213, 219, 223), (218, 215, 208), (208, 197, 138), (196, 193, 187), (159, 183, 205), (154, 179, 200), (199, 196, 190), (212, 217, 221), (197, 190, 168), (230, 227, 221), (238, 235, 229), (224, 221, 215), (233, 234, 224), (209, 206, 200), (223, 220, 213), (206, 203, 197), (212, 196, 153), (225, 215, 183), (235, 234, 230), (203, 200, 194), (202, 199, 194), (201, 198, 192), (200, 197, 191), (240, 237, 230), (220, 224, 225), (239, 237, 232), (233, 233, 230), (234, 231, 225), (195, 196, 180), (221, 218, 212), (196, 192, 180), (220, 217, 211), (219, 216, 210), (241, 238, 231), (233, 230, 223), (208, 216, 221), (231, 231, 229), (216, 213, 207), (238, 188, 35), (239, 236, 228), (214, 211, 206), (202, 183, 119), (230, 182, 34), (230, 181, 33), (188, 203, 215), (186, 200, 213), (253, 247, 115), (180, 197, 212), (178, 195, 211), (176, 194, 211), (173, 192, 210), (171, 191, 209), (169, 189, 209), (231, 228, 222), (165, 187, 208), (228, 230, 228), (162, 185, 207), (212, 209, 203), (160, 183, 206), (237, 236, 228), (237, 234, 228), (158, 182, 205), (157, 182, 205), (157, 181, 205), (209, 207, 203), (201, 209, 215), (229, 226, 220), (193, 206, 216), (190, 204, 216), (212, 208, 201), (223, 226, 226), (228, 225, 219), (218, 209, 173), (179, 189, 187), (182, 184, 184), (227, 224, 218), (236, 233, 227), (226, 223, 217), (225, 222, 216), (206, 206, 197), (202, 212, 219), (237, 235, 231), (237, 234, 225), (188, 156, 55), (196, 156, 33), (235, 232, 226), (230, 218, 207), (225, 227, 227), (204, 162, 31), (195, 169, 88), (194, 161, 57), (236, 235, 231), (232, 229, 223), (209, 208, 204), (215, 205, 169), (209, 208, 205), (210, 200, 164), (238, 235, 228), (217, 214, 208), (216, 214, 210), (230, 226, 210), (204, 195, 164), (240, 237, 232), (239, 235, 228), (235, 231, 221), (240, 190, 35), (240, 189, 35), (239, 188, 35), (201, 179, 101), (237, 187, 35), (236, 186, 35), (234, 184, 34), (223, 176, 33), (218, 172, 33), (219, 173, 32), (201, 162, 41), (208, 165, 34), (211, 167, 31), (201, 192, 161), (176, 174, 168), (166, 165, 163), (153, 169, 182), (196, 198, 197), (227, 228, 228), (191, 164, 77), (180, 167, 110), (237, 234, 227), (254, 248, 115), (185, 148, 34), (180, 147, 44), (154, 180, 204), (216, 203, 171), (34, 32, 28), (239, 189, 35), (223, 224, 224), (191, 189, 182), (227, 228, 226), (191, 192, 188), (156, 180, 204), (218, 220, 218), (238, 234, 228), (196, 193, 189), (196, 192, 186), (188, 152, 40), (240, 236, 230), (139, 149, 159), (106, 119, 131), (190, 187, 182), (155, 181, 204), (131, 133, 135), (194, 192, 186), (190, 188, 184), (239, 236, 232)]
我希望在所有图片中使用那个调色板或任何一致的调色板,但我不确定如何操作。我尝试使用PyPNG实现了以下代码:
r=png.Reader("in.png")
img = r.read()
f = open('out.png', 'wb')
w = png.Writer(img[0], img[1], palette=pal, bitdepth=8) #pal being the palette shown above
w.write(f, img[2])
f.close()
但是那看起来很糟糕。
有没有办法实现我想做的事情?希望我已经解释得足够清楚了,谢谢!