图像有损压缩与无损压缩
Modern web browsers accept four image formats by default: JPEG, GIF, PNG and SVG. But before talking about formats, let’s introduce two extremely important terms: lossless and lossy compression.
现代网络浏览器默认接受四种图像格式: JPEG , GIF , PNG和SVG 。 但是在讨论格式之前,我们先介绍两个非常重要的术语: 无损压缩和有损压缩。
Almost all images are compressed in some way. That is, the raw binary data that makes up the individual pixels and their color is packaged up and rearranged to achieve the smallest file size possible for any particular format. There are many different methods of data compression, but every system comes down to one simple question: whether it physically changes the data in the image.
几乎所有图像都以某种方式压缩。 即,构成单个像素及其颜色的原始二进制数据被打包并重新排列,以实现任何特定格式可能的最小文件大小。 数据压缩有很多不同的方法,但是每个系统都可以解决一个简单的问题:它是否物理地更改了图像中的数据。
Lossless compression does not change bits. It rearranges them, and tries to pack them into a smaller space – think of different arrangements of boxes in a moving van in an attempt to fit more inside – but does not physically change them. You obviously want a lossless compression scheme in situations for which fidelity to the original data is paramount. .zip is a ubiquitous compression scheme: bits go in, information is re-arranged and compressed, but the same bits come out after you uncompress the .zip. (You don’t want the “Z’s” in a compressed Microsoft Word document to be changed to “k’s” just because it would make the file smaller.)
无损压缩不会更改位。 它会重新排列它们,并尝试将它们包装在较小的空间中-考虑在移动的货车中尝试不同的盒子布置,以试图容纳更多的内部空间-但不会在物理上改变它们。 在原始数据的保真度至关重要的情况下,您显然希望使用无损压缩方案。 .zip是一种无处不在的压缩方案:位进入,信息被重新安排和压缩,但是在您解压缩.zip之后出现相同的位。 (您不希望将压缩的Microsoft Word文档中的“ Z”更改为“ k”,只是因为它会使文件变小。)
There are many lossless image compression formats: TIF, TGA, BMP, RAW, PNG, SVG and PSD among them. Arguably, even GIF is a lossless format. Assuming that you are feeding them the best information possible, all of those formats will preserve data completely, without loss or change. The easiest compression scheme is run-length encoding: if there are several pixels of the exact same color one after the other in a horizontal line, rather than counting them separately, GIF makes a shortcut code for them (say “five red pixels”, rather than counting “one red pixel, another red pixel…” and so on).
无损图像压缩格式很多,其中包括TIF , TGA , BMP , RAW , PNG , SVG和PSD 。 可以说,甚至GIF都是无损格式。 假设您正在向他们提供最佳信息,那么所有这些格式将完全保留数据,而不会丢失或更改。 最简单的压缩方案是游程编码:如果在水平线上有多个颜色完全相同的像素,而不是分别计数,则GIF会为它们创建快捷方式代码(例如“五个红色像素”,而不是计算“一个红色像素,另一个红色像素……”,依此类推)。
The major drawback to a lossless compression scheme (with the exception of SVG, which is predominantly a vector format) is file size. No matter how clever the algorithm, the data must be completely preserved. But what if we could change some of that data – squish it, alter it, or even throw it out – in such a way that the end user is unlikely to spot any changes?
无损压缩方案(主要是矢量格式的SVG除外)的主要缺点是文件大小。 无论算法多么聪明,都必须完全保留数据。 但是,如果我们可以更改某些数据(压缩,更改甚至丢弃),以致最终用户不太可能发现任何更改,该怎么办?
This can’t be done with Word documents… but pixels are very small. If we can change some of them to be more like their neighboring pixels, we would increase the number of shortcuts we could take in describing the image, which in turn would reduce its file size. And that’s exactly what lossy compression does. There are a few lossy compression schemes for images; JPEG is the most well-known.
Word文档无法做到这一点……但是像素非常小。 如果我们可以将其中一些更改为更接近其相邻像素,则将增加描述图像时可以使用的快捷方式的数量,从而减少其文件大小。 这正是有损压缩的作用。 有一些针对图像的有损压缩方案。 JPEG是最著名的。
Lossy compression gives significant advantages in terms of file size. However, it comes with one major caveat: the changes made to the image to achieve this compression can’t be undone. That is, the original information is lost, and can’t be retrieved. (And no amount of digital wizardry can recover it – despite what the movies and television tell you).
有损压缩在文件大小方面具有明显优势。 但是,它带有一个主要警告:对图像进行的更改以实现这种压缩是无法撤消的。 也就是说,原始信息将丢失,并且无法检索。 ( 尽管电影和电视告诉您,但没有任何数字巫术可以恢复它)。
翻译自: https://thenewcode.com/62/Images-Lossy-vs-Lossless-Compression
图像有损压缩与无损压缩