(Python) 使用 PIL 或 PyPNG 在图像中设置特定调色板

发布于 2024-11-16 14:46:01 字数 4599 浏览 2 评论 0原文

我有一堆单独处理的图像,处理后又重新组合在一起。我想将它们压缩为 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()

但结果看起来像垃圾。

有什么办法可以做我想做的事吗?希望我已经解释得足够清楚了,谢谢!

I have a bunch of images that I am working on separately, that fit back together after being worked on. I'd like to compress them to a 8 bit image in order to save size. I'm currently doing this with PIL by calling:

image = image.convert('P', palette=Image.ADAPTIVE, colors=256)

The problem is though that the palettes for each image wind up being different. I'd like to get the images all using the same palette.

For example, here is a palette I pulled out of one of the images after it had been converted and optipng-ed.

[(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)]

I'd like to use that palette, or any consistent palette, in all my images, but I'm not sure how. I tried something like this with 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()

But that turned out looking like rubbish.

Is there any way to do what I'm looking to do? Hopefully I've explained it clearly enough, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙幽之幻 2024-11-23 14:46:01

到目前为止,我发现做到这一点的唯一方法是使用 PythonMagick,这是不幸的,因为 PythonMagick 速度慢,无文档,并且如果您不使用 python-pythonmagick 包,则很难在 Linux 上安装。

colormap = PythonMagick.Image("colors256.png")
original.map(colormap)

Only way so far I have discovered to do this is using PythonMagick, which is unfortunate as PythonMagick is slow, undocumented, and difficult to install on Linux if you don't use the python-pythonmagick package.

colormap = PythonMagick.Image("colors256.png")
original.map(colormap)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文