Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 将png转换为8位位图_Python 3.x_Bitmap_Png_Python Imaging Library_8 Bit - Fatal编程技术网

Python 3.x 将png转换为8位位图

Python 3.x 将png转换为8位位图,python-3.x,bitmap,png,python-imaging-library,8-bit,Python 3.x,Bitmap,Png,Python Imaging Library,8 Bit,我尝试将8位PNG转换为8位(256索引调色板)位图图像, 但枕头是不断呕吐的糟糕结果 这就是我试过的 image = Image.open(file) image = image.convert('P') pp = image.getpalette() pp[0] = 255 pp[1] = 0 pp[2] = 255 image.putpalette(pp) 或 这就是我期望看到的结果。 这是一个实际的位图(由Photoshop完成) 枕头就是这样做的: 这是什么玩笑?! 它甚至被剪掉了

我尝试将8位PNG转换为8位(256索引调色板)位图图像, 但枕头是不断呕吐的糟糕结果

这就是我试过的

image = Image.open(file)
image = image.convert('P')
pp = image.getpalette()
pp[0] = 255
pp[1] = 0
pp[2] = 255
image.putpalette(pp)

这就是我期望看到的结果。 这是一个实际的位图(由Photoshop完成) 枕头就是这样做的: 这是什么玩笑?! 它甚至被剪掉了 我应该如何正确转换它

原始图像:


您可以这样做:

from PIL import Image

# Open image
image = Image.open('feather.png')

# Quantize to 256 colours using fast octree method
result = image.quantize(colors=256, method=2)

您可以这样做:

from PIL import Image

# Open image
image = Image.open('feather.png')

# Quantize to 256 colours using fast octree method
result = image.quantize(colors=256, method=2)

你能发布原始图像,以便我们可以对其进行测试吗?哦,好的,谢谢:)你能发布原始图像,以便我们可以对其进行测试吗?哦,好的,谢谢:)@MarkSetchell回答得很好(+1)顺便问一下,你能解释一下
image.quantize()是如何实现的吗
工作方式不同,然后通过
convert(“P”)
将图像转换为
P
模式。其次,快速八叉树方法在代码中做了什么?我在运行代码时没有使用
quantize()
中的
method=2
参数,得到了相同的结果。@VasuDeo.S您好Vasu,很抱歉,我与PIL作者没有联系,也没有深入了解他们为什么选择任何特定方式。我只是有一些使用PIL/Pizz的经验,这是我学会获得最佳效果的方法。我现在检查了你的方法,它只在PNG中有效,但当我尝试保存为“P”类型位图时,同样的情况也会发生。我想这是因为alpha通道。alpha通道没有被顺利删除。@MarkSetchell回答得很好(+1)顺便问一下,你能解释一下
Image.quantize()
的工作原理,然后通过
convert(“P”)
将图像转换为
P
模式吗。其次,快速八叉树方法在代码中做了什么?我在运行代码时没有使用
quantize()
中的
method=2
参数,得到了相同的结果。@VasuDeo.S您好Vasu,很抱歉,我与PIL作者没有联系,也没有深入了解他们为什么选择任何特定方式。我只是有一些使用PIL/Pizz的经验,这是我学会获得最佳效果的方法。我现在检查了你的方法,它只在PNG中有效,但当我尝试保存为“P”类型位图时,同样的情况也会发生。我想这是因为alpha通道。alpha通道未顺利移除。。