JPEG image quality determination

I allow users to upload images. However, I want to keep JPEG quality no more than 90%. What I plan to do is determine the current quality: - If less than 90% do nothing - If more than 90%, than using Image Magick to recompress the image to 90%

Can this be done? I prefer PHP, but any language will help.

+4
source share
6 answers

paddy correctly, that this option is not always saved in the JPEG file. If so, you can use identify from Imagemagick to read quality. For instance:

 $ identify -format '%Q' tornado_ok.jpg 

93%

Update: based on the answer to this question https://superuser.com/questions/62730/how-to-find-the-jpg-quality, I that the identify team can still determine the quality by reverse engineering the quantization tables, even if the whole image EXIF / other metadata is lost. By the way, the title of your question now, is a possible duplication of this question with which I am associated.

But for me, your question has its merits, because in your question text you explain what you are trying to do, which is more than just determining the quality of jpeg. However, you should probably update the title if you want you to think that you are trying to solve a more specific problem than just reading a JPEG image.

If you do not archive the original images, even 90% is excessive for network use. 75% were standard in the old days (degradation was visible only with close inspection between images side by side), and now in days of high throughput 85% is a very high-quality option. The difference of 5% quality between 90% and 85% is almost invisible, but as a rule, it will save you more than 30%. The JPEG algorithm is designed to start by eliminating information that is not visible to humans in the first stages of compression (above 80% or so).

Update / note: the compression quality parameters I'm talking about are from tests with libjpeg, a very widely used JPEG library. in Photoshop, compression ratios and other software quality parameters are independent and do not necessarily mean the same as libjpeg settings.

The idea of ​​using image height and image width to calculate the acceptable file size is reasonable:

You can get the height / width of the image as follows:

 list($originalWidth, $originalHeight) = getimagesize($imageFile); 

My own high-quality photos posted on the Internet, for example: http://ksathletics.com/2013/wsumbb/nw.jpg are usually stored at a ratio of about 200 KB per megapixel.

So, for example, you can multiply the width by height and divide by 1,000,000 to calculate megapixels in the image. Divide the file size by 1024 to calculate KB. Then divide the resulting KB by megapixels. If the result is less than 200 or any value that you decide, then you do not need to compress it again. Otherwise, you can re-compress it with 85% quality or whatever quality you decide.

+6
source

What? I faced the same problem with the application that I am developing ... My problem is that I am extracting several images from a random site and each element has several images, I would like to show one image for each element and bring to the user image of the best quality.

I came up with this idea, its quite simple and will work for any language and any type of compression:

 //Parameters you will need to retrieve from image $width = 0; $height = 0; $filesize = 0; //Quality answer for your image $quality = (101-(($width*$height)*3)/$filesize); 

I executed this algorithm against http://fotoforensics.com/tutorial-estq.php mentioned above, and here are the results:

 Filename Width Height Pixels BitmapBytes FileBytes Quality estq-baseline.png 400 300 120000 360000 163250 98,79 estq-90.jpg 400 300 120000 360000 34839 90,67 estq-80.jpg 400 300 120000 360000 24460 86,28 estq-70.jpg 400 300 120000 360000 19882 82,89 estq-25.jpg 400 300 120000 360000 10300 66,05 

The main idea of ​​this algorithm is to compare the size that an image can achieve if it is written in a raster way (without compression, 3 bytes per pixel, 3 bytes for RGB) to the size currently used by this image. The smaller the image size, the higher the compression will be, regardless of the compression method used, but rather its JPG, PNG or something else, which will lead to a larger gap or a smaller gap between the uncompressed and compressed image.

It is also important to note that this is a mathematical solution for comparison, this method will not return the actual image quality, it will answer the percentage ratio between uncompressed and compressed sizes!

If you need more information, you can send me an email: rafaelkarst@gmail.com

+5
source

Since the OP has stated that it prefers php, I suggest the following:

 $img = new Imagick($filename); $quality = $img->getImageCompressionQuality(); 
+2
source

You cannot guarantee that the quality setting is stored in JPEG metadata. This is an encoder parameter, not an image attribute.

Learn more about JPEG Quality Assessment.

It may make sense to simply determine the maximum file size. In the end, image quality limitations are associated with bandwidth savings. Therefore, the ratio between image size and file size is more appropriate.

+1
source

For those using GraphicsMagick instead of ImageMagick, you can get JPEG quality with the following command:

 gm identify -format '%[JPEG-Quality]' path_to/image_file.jpg 

and according to the documentation http://www.graphicsmagick.org/GraphicsMagick.html#details-format

Please note that JPEG does not have the concept of “quality” and that the quality metric used and evaluated by the software is based on the quality metric set by IJG JPEG 6b. Other encoders (such as those used by Adobe Photoshop) use different encoding metrics.

0
source

If your jpeg was created using direct scaling of standard image quantization tables, and the quality 0-100 was used based on the Independent JPEG Group formula, then assuming that you have brightness quantization tables in an array called quantization (for example, the PIL PILON module provides in image.quantization[0] ), then the original value can be obtained using:

 if quantization[58] <= 100: originalQuality = int(100 - quantization[58] / 2) else: originalQuality = int(5000.0 / 2.5 / quantization[15]) 

In principle, the default brightness quantization value of # 15 is 40 and # 58 is 100, so they get convenient values ​​for extracting results. IJG scales values ​​around 50 across 5000 / Q and below 50 across 200 - 2 * Q If the quality setting is less than 8, this will not give decent results (if quantization[5] == 255 ) - in this case, perhaps use the position of quantization table # 5

See https://tools.ietf.org/html/rfc2435#section-4.2 .

0
source

Source: https://habr.com/ru/post/1492220/


All Articles