通过保留半透明像素,使用ImageMagick将32位png转换为8位png [英] Convert 32 bit png to 8 bit png with ImageMagick by preserving semi transparent pixels

查看:667
本文介绍了通过保留半透明像素,使用ImageMagick将32位png转换为8位png的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用ImageMagick将32位png转换为8位png,但半透明像素会丢失。如何解决这个问题呢?我使用的命令是以下

  convert original.png PNG8:output.png 


解决方案

显然,即使PNG格式实际上允许8-中的任何和所有颜色位索引颜色PNG完全或部分透明,仅限ImageMagick的PNG8




从左到右:


  1. 原始32位RGBA PNG

  2. pngquant 256< input.png> output.png

  3. 量化转换input.png -colors 255 output.png *

  4. 使用

  5. 使用 convert input.png -colors 255 PNG8:output.png *






*)在下面的评论中,建议 -colors 255 是必要的为'背景'颜色保留一个条目。根据我的测试,我没有注意到实际情况;使用 -colors 256 仍将生成一个8位色彩映射的PNG ,量化伪像与 -colors 255 -colors 255的输出定性相似,但细节不同code>。尽管如此,为了安全起见,我已经使用 -colors 255 作为上述示例。将色彩图大小减少一种颜色本身不会显着影响结果的质量,因为 pngquant 255 的测试将演示


I want to convert 32 bit png to 8 bit png with ImageMagick, but semi transparent pixels are lost. How to solve this problem? The command that I am using is the following

convert original.png PNG8:output.png

解决方案

Apparently, even though the PNG format actually allows any and all of the colors in an 8-bit indexed color PNG to be fully or partially transparent, ImageMagick's "PNG8" format specifier only supports GIF-style 1-bit transparency.

It's possible to produce indexed 8-bit PNGs using ImageMagick by not using the PNG8: specifier, but simply using -colors 256 or -colors 255* to reduce the number of colors in the image. Unfortunately, at least based on my tests using ImageMagick 6.8.9, the resulting images have some rather weird and quite unnecessarily ugly color quantization artifacts.

Fortunately, there's a much better tool for this specific job: pngquant. Using it, converting a 32-bit RGBA PNG into an 8-bit colormapped PNG with minimal quality loss is as easy as:

pngquant 256 < original.png > output.png


As a quick demonstration, here's a simple test picture (a star with a semitransparent drop shadow) converted to 8-bit PNG using various methods:

From left to right:

  1. Original 32-bit RGBA PNG
  2. Quantized with pngquant 256 < input.png > output.png
  3. Quantized with convert input.png -colors 255 output.png*
  4. Quantized with convert input.png PNG8:output.png
  5. Quantized with convert input.png -colors 255 PNG8:output.png*


*) In the comments below, it is suggested that -colors 255 is necessary "to reserve one entry for the 'background' color." Based on my testing, I have not observed this to actually be the case; using -colors 256 will still produce an 8-bit colormapped PNG, with quantization artifacts qualitatively similar to, but differing in details from, the output with -colors 255. Nonetheless, just to play it safe, I've used -colors 255 for the examples above. Reducing the colormap size by one color should not, in itself, significantly affect the quality of the results, as a test with pngquant 255 will demonstrate.

这篇关于通过保留半透明像素,使用ImageMagick将32位png转换为8位png的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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