使用 pypng 将 24 位 PNG 文件转换为 8 位颜色索引图像
Convert 24-bit PNG files to 8-bit color indexed images with pypng
我正在尝试编写一个 python 脚本,该脚本接受标准的 24 位 png 并将它们转换为 8 位 png 以获得更好的压缩效果。看起来 pypng 可以做到这一点,但我不太清楚如何使用它。图像处理对我来说是一个新领域,所以这看起来很傻。我目前有这个:
r=png.Reader(<myfile>)
test = r.asRGBA8()
这给了我 return 中的元组(我相信图像的层)。但是我似乎无法将其写回或保存回图像。我错过了什么? Here's a test image
原答案
我认为这符合您的要求:
from PIL import Image
# Load image
im = Image.open('logo.png')
# Convert to palette mode and save
im.convert('P').save('result.png')
更新答案
结果我找不到让 PIL 制作合理的调色板图像的方法,但可以通过其他几种方法来实现...
或者像这样 wand
:
#!/usr/bin/env python3
from wand.image import Image
with Image(filename='logo.png') as img:
img.quantize(number_colors=256, colorspace_type='lab', treedepth=0, dither=False, measure_error=False)
img.save(filename='result.png')
或者,通过在命令行输入 ImageMagick 并执行:
magick logo.png -colors 255 png8:logo8.png # use "convert" in place of "magick" if using v6
最新答案
好的,我找到了一种让 PIL/Pillow 做得更好的方法,正如预期的那样,它利用了通常不内置在 Pillow 中的 libimagequant
(至少在 macOS 上)我是)。代码如下所示:
#!/usr/bin/env python3
from PIL import Image
# Load image
im = Image.open('logo.png')
# Convert to palette mode and save. Method 3 is "libimagequant"
im.quantize(colors=256, method=3).save('result.png')
在 macOS 上使用 libimagequant
构建 PIL/Pillow 的步骤如下 - 它们在其他平台上会有所不同,但您应该能够了解总体思路并进行调整:
pip uninstall pillow # remove existing package
brew install libimagequant
brew install zlib
export PKG_CONFIG_PATH="/usr/local/opt/zlib/lib/pkgconfig"
pip install --upgrade Pillow --global-option="build_ext" --global-option="--enable-imagequant" --global-option="--enable-zlib"
关键字:Python,图像处理,PIL/Pillow,libimagequant,macOS,量化,量化。
我正在尝试编写一个 python 脚本,该脚本接受标准的 24 位 png 并将它们转换为 8 位 png 以获得更好的压缩效果。看起来 pypng 可以做到这一点,但我不太清楚如何使用它。图像处理对我来说是一个新领域,所以这看起来很傻。我目前有这个:
r=png.Reader(<myfile>)
test = r.asRGBA8()
这给了我 return 中的元组(我相信图像的层)。但是我似乎无法将其写回或保存回图像。我错过了什么? Here's a test image
原答案
我认为这符合您的要求:
from PIL import Image
# Load image
im = Image.open('logo.png')
# Convert to palette mode and save
im.convert('P').save('result.png')
更新答案
结果我找不到让 PIL 制作合理的调色板图像的方法,但可以通过其他几种方法来实现...
或者像这样 wand
:
#!/usr/bin/env python3
from wand.image import Image
with Image(filename='logo.png') as img:
img.quantize(number_colors=256, colorspace_type='lab', treedepth=0, dither=False, measure_error=False)
img.save(filename='result.png')
或者,通过在命令行输入 ImageMagick 并执行:
magick logo.png -colors 255 png8:logo8.png # use "convert" in place of "magick" if using v6
最新答案
好的,我找到了一种让 PIL/Pillow 做得更好的方法,正如预期的那样,它利用了通常不内置在 Pillow 中的 libimagequant
(至少在 macOS 上)我是)。代码如下所示:
#!/usr/bin/env python3
from PIL import Image
# Load image
im = Image.open('logo.png')
# Convert to palette mode and save. Method 3 is "libimagequant"
im.quantize(colors=256, method=3).save('result.png')
在 macOS 上使用 libimagequant
构建 PIL/Pillow 的步骤如下 - 它们在其他平台上会有所不同,但您应该能够了解总体思路并进行调整:
pip uninstall pillow # remove existing package
brew install libimagequant
brew install zlib
export PKG_CONFIG_PATH="/usr/local/opt/zlib/lib/pkgconfig"
pip install --upgrade Pillow --global-option="build_ext" --global-option="--enable-imagequant" --global-option="--enable-zlib"
关键字:Python,图像处理,PIL/Pillow,libimagequant,macOS,量化,量化。