如何在golang中了解图像的当前“质量”?我需要减小文件大小。

huangapple go评论85阅读模式
英文:

How to know the current "quality" of an image in golang? I need to reduce the file size

问题

我正在尝试在Go语言中减小/压缩图像文件的大小。当我使用jpeg.Encode函数,并将Quality参数设置为75时,一个10MB的图像会变成18MB...我不明白为什么会这样,这个库中的Quality参数是什么概念?

另外,我想知道在编码之前如何确定原始图像的Quality,我认为如果我知道这个值,我可以将Quality设置为更小的值,或者尝试其他方法,比如将图像的高度和宽度减小10%。

我希望能够理解jpeg.Encode函数中options参数的Quality属性的标准。我想获取更多关于图像文件大小以及如何减小文件大小的知识。

英文:

I'm trying to reduce/compress the file size of an image in golang. When i use jpeg.Encode and pass the options as param with Quality 75, an image of 10mb turns into a 18mb image.... I don't understand why, which is the concept of quality in this library?.

Also I want to know how can i know before encoding which is the original Quality, i think if i know that i can set it with smaller value or maybe trying another way like resizing the image 10% less of height and width.

I expect to understand the criteria of the Quality atribute of options param in jpeg.Encode function. I want to acquire more knowledge of image files size and how to reduce it.

答案1

得分: 2

JPEG质量参数不是图像的特征,而是JPEG编码过程的特征。由于JPEG是一种有损编码,意味着编码后的图像无法解码为完全相同的原始图像,您可以指定编码器尽力复制原始图像的程度。质量100几乎没有损失,较低的质量会使编码器不那么努力。因此,质量参数越大,文件大小越大,因为编码器试图保留更多信息。

为了直观地理解为什么编码文件可能比原始文件更大,这里有一个关于人类语言中的“压缩”示例(不是JPEG,但从信息理论的角度来看,原理相同):

00000000000000000000000000000000000000000000000000 可以描述为“五十个零”,压缩比为50:12(即源长度缩小了四倍以上)。

01101 是“一个零,两个一,一个零和一个一”,压缩失败,压缩比为5:34,编码值几乎是原始值的七倍。

这张照片(来自JPEG的维基百科页面)展示了质量参数对结果图像的影响,从左侧的0到右侧的100:

如何在golang中了解图像的当前“质量”?我需要减小文件大小。

编码器在尝试对图像进行编码之前无法知道结果如何;这在很大程度上取决于图像本身。JPEG在编码颜色渐变方面非常出色,但在处理锐利边缘方面非常糟糕。因此,它可以很好地压缩照片,但在处理静态噪声等方面效果极差。

这意味着除非您的图像已经经过JPEG编码,否则不存在“编码之前的原始质量”。如果图像已经经过JPEG编码,并且您尝试重新对其进行编码,您通常不会看到文件大小增加而几乎没有收益(因为编码器试图复制不仅图像,还有先前编码的伪影),就像您尝试压缩已经压缩过的文件一样。对已经以其他方式编码的图像进行转码可能会导致文件大小减小,也可能不会,这取决于图像和先前使用的编码方式。

英文:

JPEG quality parameter is not a feature of the image, but a feature of JPEG encoding process. Since JPEG is a lossy encoding, meaning that the encoded image cannot be decoded into the exact original, you can specify to the encoder how faithfully it should try to replicate the original. Quality 100 should have almost no loss, lesser qualities will have the encoder not try so hard. Consequently, the larger the quality parameter, the larger the file size, as the encoder tries to preserve more information.]

To intuitively understand why an encoded file could end up larger than the original, here's an example of "compression" in human speech (not JPEG, but from information theory standpoint, same principles apply):

00000000000000000000000000000000000000000000000000 can be described as "fifty zeroes", a compression of 50:12 ratio (i.e. the source length was reduced by over four times).

01101 is "a zero, two ones, a zero and a one", a failure of compression at 5:34 ratio, the encoded value being almost seven times the original.

This photo (from Wikipedia page on JPEG) shows the effect of the quality parameter on the resulting image, from 0 on the left to 100 on the right:

如何在golang中了解图像的当前“质量”?我需要减小文件大小。

The encoder can't know without trying to encode an image what the result will be; that depends largely on the image. JPEG is very good at encoding colour gradations, and very bad at sharp edges. Thus, it compresses photographs really well, but will be extremely bad at e.g. static noise.

This means there is no "original quality before encoding", unless your image is already JPEG-encoded. If an image is already JPEG-encoded, and you try to re-encode it, you will typically see few to no gains for an increase in size (as the encoder tries to replicate not just the image, but also the artefacts from the previous encoding), just like if you try to zip an already zipped file. Transcoding an image that is already encoded in some other way may or may not result in decrease in file size, depending on both the image and the previously used encoding.

huangapple
  • 本文由 发表于 2023年3月16日 12:00:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75752061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定