image - 为什么颜色反转时 1bpp PNG 图像文件大小会增加?

标签 image compression png

当我生成每像素索引颜色为 1 位的黑白 png 图像时,如果我选择在白色背景上使用黑色背景,文件大小似乎会有明显差异。附加的图像应该具有完全相同的信息,并且都是由 .NET 4 的 Bitmap.Save() 函数创建的。

为什么压缩后白色背景的图像明显变大?

黑色背景:4.62KB
Black background 4.62KB

白底:5.67KB
White background: 5.67KB

最佳答案

答案可能令人惊讶,但非常简单,并且在于 PNG 格式本身。

如前所述,PNG 对每一行应用过滤器。这有助于压缩在线条内或相邻线条之间具有图案的图像。在这种情况下,图像几乎是单色的,只有几个点,所以不使用过滤器会产生最好的结果。

filter byte is stored as the first byte每个图像行。这是关键。

The filter step itself results in a sequence of bytes of the same size as the incoming sequence, but in a different representation, preceded by a filter type byte. [...] The filter type byte is not considered part of the image data, but it is included in the datastream sent to the compression step.



不使用过滤器时,过滤器字节为 0x00 .黑色 1 位 4x4 图像的示例,过滤器字节加粗。

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
压缩器接收到的与平面序列相同的图像。

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
并且像素反转的图像。

0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1
很明显哪个压缩更好。

在提供的白色背景图像中,流 0x01经常被 0x00 打断过滤字节,对可压缩性有负面影响。在黑色背景图像中,没有这种效果。

关于image - 为什么颜色反转时 1bpp PNG 图像文件大小会增加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25459165/

相关文章:

java - 获取24位PNG像素值

java - Swing ,调整大小然后绘制

python - 用于图像数据集的 scikit-learn PCA

swift - iOS 上的 ImageRenderer - 生成模糊图像

ios - 如何找出 NSData 数组的长度?

c# - C# 中的可写字节流风格功能

apache - 为什么我的托管公司不支持 mod_deflate?

java:在运行时跟踪文件大小?

binary - 解释 PNG 像素数据

javascript - 将 SVG 转换为 PNG 时包括字体