使用pypng将24位PNG文件转换为8位彩色索引图像 [英] Convert 24-bit PNG files to 8-bit color indexed images with pypng

查看:1064
本文介绍了使用pypng将24位PNG文件转换为8位彩色索引图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个采用标准24位png并将其转换为8位png以便更好压缩的python脚本.看起来pypng可以做到这一点,但我还不太清楚如何使用它.图像处理对我来说是一个新领域,因此这似乎很愚蠢.我目前有这个:

I'm trying to write a python script that takes in standard 24-bit pngs and converts them to 8-bit pngs for better compression. It looks like pypng can do this but I can't quite figure out how to use it. Image manipulation is a new area for me so this may seem silly. I have this currently:

r=png.Reader(<myfile>)
test = r.asRGBA8()

这给了我元组作为回报(我相信图像的层次).但是我似乎无法将其写或保存回图像.我想念什么? 这是一张测试图像

This gives me tuples in return (the layers of the image I believe). However I can't seem to write or save this back to an image. What am I missing? 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制作出合理的调色板图像,但是可以通过其他几种方法来实现...

I can't find a way to get PIL to make a sensible palette image as a result, but can do it a couple of other ways...

使用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 并执行以下操作:

Or, by shelling out to ImageMagick at the command-line and doing:

magick logo.png -colors 255 png8:logo8.png      # use "convert" in place of "magick" if using v6

最新答案

好吧,我找到了一种使PIL/Pillow更好地工作的方法,并且正如预期的那样,它利用了通常不内置在Pillow中的libimagequant(至少在我所在的macOS上).代码如下:

Ok, I found a way to get PIL/Pillow to do a better job, and as expected, it makes use of libimagequant which is not normally built into Pillow (at least on macOS where I am). The code looks like this:

#!/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的步骤如下-它们在其他平台上会有所不同,但是您应该能够获得一般的想法并进行调整:

The steps, on macOS to build PIL/Pillow with libimagequant are as follows - they will differ on other platforms but you should be able to get the general idea and adapt:

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/枕头,libimagequant,macOS,量化,量化.

Keywords: Python, image processing, PIL/Pillow, libimagequant, macOS, quantise, quantize.

这篇关于使用pypng将24位PNG文件转换为8位彩色索引图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆