为什么缩小 PNG 图片后占用的存储空间反而变大

PNG 的压缩方式

前些天处理图片的时候发现 PNG 图片缩小尺寸后反而比原图占用更多的空间,搜了一下 PNG 的压缩方式,找到这个链接:

https://stackoverflow.com/a/14147001

部分原文:

PNG is a lossless format and compresses the image data by first performing a step called prediction and then applying the same algorithm used in zlib. The prediction step is a crucial one in order to effectively compress the file, and it is based on the values of earlier neighbors pixels.

So, suppose you have a large PNG in black & white (by that I really mean only black and white, some people confuse that by grayscale sometimes). Also suppose it is not a tiny checkerboard pattern. In many regions of this image, you will have a relatively large white region, and then a relatively large black region, and so on. When the predictor is inside one of these large regions, it has no trouble to correctly predict that the current pixel intensity is exactly equal to the last one. This makes it easier to better compress the data describing your image.

大概意思就是如果存在连续的相同色块,可以压缩到一起。

解释

比如连续 10 个白色像素,不需要存储 白色, 白色, 白色, ...,可以存储 白色 * 10。颜色数量越少,连续相同颜色的区域越大,压缩效果就越好。

当调整图片尺寸的时候,为了抗锯齿,原图的 白色 * 10, 黑色 * 10 可能会变成 白色 * 5, 浅灰, 灰色, 深灰, 黑色 * 5,虽然像素数变少了,但增大了存储需要的空间。

解决方案

如果原图和新图的尺寸差距很小,可以使用 NEAREST(最邻近插值法)保存图片,尽量减少过度色的增加,缺点是尺寸差距越大,锯齿感越强,甚至会丢失部分线条。

如果尺寸差距很大,增加过度色造成的影响远不如合并像素节省的空间,可以使用复杂的插值法提高缩略图的保真度。

  • 467

涨姿势了。

回复内容较长可以拖右下角↑
回到
顶部