python - 如何在Pillow中的Python中将24位深度更改为8位深度?

标签 python python-imaging-library

我有一个1.png,它是24位深度。我想使用带有pillow的python将其转换为8位深度 .

我尝试了这段代码,但是当我检查这两个图像(1.png 和 2.png)时,它们仍然是 24 位深度:

from PIL import Image
import os
os.chdir('D:\\test')
img=Image.open('1.png')
img.convert("P", palette=Image.ADAPTIVE, colors=8)
img.save('D:\\test\\2.png')
 

图像:images

如何将 1.png 转换为 8 位深度并将其保存在枕头中?

最佳答案

我解决了我的问题,但我不知道为什么以前的代码不起作用!

我将 img.convert("P", Palette=Image.ADAPTIVE, Colors=8) 添加到新变量中。 并使用 a.save('D:\\test\\2.png')

保存它
from PIL import Image
import os
os.chdir('D:\\hash lip\\test')
img=Image.open('1.png')
a=img.convert("P", palette=Image.ADAPTIVE, colors=8)
a.save('D:\\test\\2.png')
 

关于python - 如何在Pillow中的Python中将24位深度更改为8位深度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71733860/

相关文章:

python - 如何在 Pycharm 2018.2 中的类方法声明中添加带有类型提示的文档?

python - 使用 Python 进行简单的 JSON 编码

python - Tornado 和 PIL : open image from headers

python - Pythons PIL (v2.6) 中的 TypeError 消息 : integer expected, float

php - 如何将图像从 python 传递到 laravel

python - 图像裁剪工具(Python)

python - 如何在 Django 中为 css 文件设置相对 url

python - 尝试创建 python Web 应用程序时 Vagrant 出现错误

python - zeromq pub/sub with multipart 不工作

image - 这种裁剪算法更优雅的改写是什么? (在 Python 中)