php Hello Xiaobian Yuzai! In golang, if you want to know the current quality of an image and reduce the file size, you can use the DecodeConfig function in the image/jpeg package to get the basic information of the image, including the image's width, height, color mode, etc. You can then use the Resize function from the image package to resize the image, thus reducing the file size. In addition, you can also use the Options structure in the image/jpeg package to set the quality parameters of the image, and further reduce the file size by adjusting the value of the quality parameters. Hope this short answer helps!
I am trying to reduce/compress the file size of images in golang. When I use jpeg.Encode and pass the option as argument for quality 75, the 10mb image becomes a 18mb image...I don't understand why, is this the concept of quality in this library?
Also, I want to know how to know which is the original quality before encoding, I think if I know I can set it to a smaller value, or try something else like resizing the image to 10 for the height and width %.
I would like to know the criteria for the quality attribute of the option parameter in the jpeg.Encode function. I would like to gain more knowledge about image file size and how to reduce image file size.
The JPEG quality parameter is not a characteristic of the image, but a characteristic of the JPEG encoding process. Since JPEG is a lossy encoding, meaning that the encoded image cannot be decoded to the exact original image, you can specify to the encoder how faithfully it should try to replicate the original image. Quality 100 should see little loss, lower quality encoders won't work as hard. Therefore, the larger the quality parameter, the larger the file size because the encoder tries to retain more information. ]
To intuitively understand why the encoded file may end up being larger than the original file, here is an example of "compression" in human speech (not JPEG, but from an information theory perspective, the same principles apply):
0000000000000000000000000000000000000000000000000
can be described as "fifty zeros", a 50:12 ratio compression (i.e. the source code length is reduced by more than four times).
01101
is "one zero, two ones, one zero and one one", the 5:34 ratio compression failed, and the encoded value is almost seven times the original.
This photo (from the Wikipedia page on JPEG) shows the quality parameters versus the resulting image, from 0 on the left to 100 on the right:
The encoder has no way of knowing what the result will be without trying to encode the image; it depends a lot on the image. JPEG is very good at encoding color gradients, but very good at encoding sharp edges. So it can compress photos very well, but it can be very bad at compressing photos and so on. static noise.
This means there is no "original quality before encoding" unless your image has been JPEG encoded. If the image is already JPEG encoded and you try to re-encode it, you will usually find little to no gain in size (because the encoder not only tries to copy the image, but also the image that was previously encoded), like you Same as trying to compress an already compressed file. Transcoding an image that has been otherwise encoded may or may not result in a reduced file size, depending on the image and the encoding previously used.
The above is the detailed content of How to know the current 'quality' of an image in golang? I need to reduce file size. For more information, please follow other related articles on the PHP Chinese website!