16
\$\begingroup\$

I have a couple of JPEGs. Is there a way to find out which compression level was used when encoding them? That is, were they encoded at level 60 or 90?


The reason I'm asking is because I have a site where I use compressed images, which was compressed in a particular program. Now I want to compress the reset of my images in the same program and at the same level so they will kind of look "as bad" as the other ones :)

\$\endgroup\$
0

3 Answers 3

19
\$\begingroup\$

No, you can not and it does not make sense to do so, since there is no ubiquitous definition of the JPEG compression level. The actual result when saving a JPEG with compression level 60 in one software can differ significantly from what another software produces when set to level 60.

If you use ImageMagick as suggested by Rolazaro Azeveires, it will indeed print a quality-number, but this number is not based on EXIF data. ImageMagick calculates its own quality-index based on the number of quantization tables in the image file and this number may again differ from the quality level you actually set when saving the image.

For example, if you have a structurally simple image, the simplest being a single-coloured tile, and you save it at level 100 with a software using a perceived quality index, the software can save the image essentially lossless with only a small number of quantization tables. Analyzing the file later with ImageMagick is likely to give you a very low quality level, since ImageMagick only sees a few quantization tables and does not realize that actually only a small number of quantization tables was necessary to save the file at a high quality setting.

Here are for example the quality reported by ImageMagick for some different options when saving an image from Photoshop:

Photoshop                 ImageMagick identify -verbose
save for web q=1/100         Quality: 55
save for web q=80/100        Quality: 94
save for web q=100/100       Quality: 99
save image q=1/12            Quality: 81
save image q=6/12            Quality: 92
save image q=12/12           Quality: 99

As you can see, even within one software, two different functions 'save image' and 'save for web and devices' are both using different quality level scales and neither match the quality level reported by ImageMagick.

Addition: Since Chris H tries to argue that the quality level as reported by ImageMagick can be useful in some cases, I will show some examples why that is not the case. The only reasonable way to judge the quality of a JPEG file is to look at it and not to rely on some magic number procuded by a software tool.

Let's first look at this JPEG file. Yes, the quality is not exceptionally good, there are some visible artifacts and ImageMagick reports a quality of 62 while the file size is 17.426 bytes:

enter image description here

Let us assume that we have the same image from a different source and for this file, ImageMagick reports a quality of 99 and the file size is 21.470 bytes:

enter image description here

It should IMHO be very obvious that neither file size nor ImageMagick's quality level are suitable to judge the true quality of an image file.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ You mean kilobytes, not bytes. Also, if you saved the image with a very low quality, then re-saved those artefacts with very high quality, then that's not a very relevant test for most use cases? \$\endgroup\$
    – Chris
    Apr 22, 2020 at 11:19
  • \$\begingroup\$ @Chris I have no idea what you mean. I mean 'bytes' everywhere, where I have written 'bytes' in my answer. \$\endgroup\$
    – jarnbjo
    Apr 22, 2020 at 14:29
  • \$\begingroup\$ Ahh, apologies, you're using . as a thousand separator rather than a decimal place. My point on relevance is that your Photoshop quality is indicated by the ImageMagick quality, so could be useful, unless you're trying to catch someone who has deliberately saved lossy then re-saved less lossy. Assuming this isn't the case for the OP, they could experiment to find settings to get a similar quality level from ImageMagick. \$\endgroup\$
    – Chris
    Apr 22, 2020 at 14:52
  • \$\begingroup\$ @Chris There is no need to deliberately do anything stupid to run into the problem I am describing. Yes, I have resaved the image with a better quality than in a previous save operation, but that was just to show the effect. That is not the only way to run into the problem. The point was just to show that there is no relation between the number of quanitzation tables in or the file size of a JPEG file and the perceived image quality. \$\endgroup\$
    – jarnbjo
    Apr 22, 2020 at 15:47
  • \$\begingroup\$ For more confirmation that the quality numbers are not comparable across softwares see faqs.org/faqs/jpeg-faq/part1/section-5.html from the jpeg creators themselves. \$\endgroup\$ Feb 18, 2021 at 23:14
10
\$\begingroup\$

You can, sort of. ImageMagicks' identify command can show a estiamte

identify -verbose image.jpeg

will produce (a lot of) information about the image.

One of the lines will be something like:

Quality: 84

If that command shows too much, and you only need the quality, you may pipe it with grep:

identify -verbose image.jpeg | grep Quality

(Stolen and expanded from "How to find the jpg quality", at superuser.com)

(edited, to include that it is not EXIF data but a guesstimate, and to say "go and read the other nice, and probably better, answer" :-)

\$\endgroup\$
0
3
\$\begingroup\$

Two answers: Identify & Judge

Quick summary:

identify -format "%f: %Q\n"  a.jpg  b.jpg 
jpegjudge  a.jpg  b.jpg

Identify

For the particular task in this question — determining a reasonable quality to save at so things look roughly the same quality — the answer by Rolazaro Azeveires is more than sufficient: just use ImageMagick's guesstimate of image quality with the identify command. For example:

identify -format "%f %Q%% (%wx%h, %B bytes)\n"  a.jpg  b.jpg 

Output:

a.jpg: 62% (500x350, 17285 bytes)
b.jpg: 99% (500x350, 21353 bytes)

The ImageMagick algorithm works reasonably well and is fast.

Judge

A good question is: How easily fooled is ImageMagick when given typical images? @jarnbjo answers that not only is ImageMagick's algorithm useless, but the problem is inherently subjective and only a human can compare images for quality. I respectfully disagree.

While I see @jarnbjo's point about humans being the ultimate judge, I do not think using software is unreasonable. I have found ImageMagick's quality assessment to be useful in my own work, but it sometimes fails on images I've received from others. Typically, these are images that have been reprocessed multiple times by many different people and/or automated systems.

When I need a more robust heuristic, I pull out judge (also known as JPEG Judge) which uses a statistical analysis of the image to assign a "quality" from 0 to 1000. Despite being a very old program, it still works so I still use it.

For example, given the images a.jpg and b.jpg which @jarnbjo used in his answer, we can run a test like so:

jpegjudge  a.jpg  b.jpg

And the output shows that the image that looks better to us humans is also favored by this algorithm:

a.jpg: 319
b.jpg: 192

Here's a screenshot of an image comparison program which shows the output for both ImageMagick's quality assessment and JPEG Judge. Two images of the same photo with statistics shown beneath

Notes & Caveats

  1. While ImageMagick and Judge are not UNIX specific commands, they are often bundled with UNIX-like OSes. For example:

    • Debian GNU/Linux: apt install imagemagick jpegjudge
      Ubuntu & Mint: same as Debian.
    • Apple MacOS: brew install imagemagick , JPEG Judge must be compiled by hand.

    Microsoft Windows does not yet have those tools in the Microsoft Apps Store, but they can be installed manually.

  2. Unlike ImageMagick's identify, JPEG Judge only operates on JPEG files and they must be of identical size.

  3. While converting to a lossless format and back to JPEG does not affect the metric much, converting a JPEG to another lossy format, such as WEBP, and back may fool JPEG Judge. This makes sense as the errors are statistically different from errors caused by the JPEG format.

  4. Judge is a very old program and there are surely better metrics available by now. Please let me know in the comments if you know of any.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Nice! Thanks! Mac users can install ImageMagick using Homebrew (brew.sh) instead of apt. \$\endgroup\$ Jul 10, 2022 at 18:28
  • \$\begingroup\$ Thanks, @JanSteinman. I've edited the answer to mention MacOS. It looks like Mac users cannot yet brew install jpegjudge and would have to compile by hand. I don't like having to tell people to do that. Would you be able to add a "cask" for it? \$\endgroup\$
    – hackerb9
    Jul 14, 2022 at 16:08
  • 1
    \$\begingroup\$ Managed to compile it, but I haven't figured out how to cask it, or where to put it afterward. \$\endgroup\$ Jul 16, 2022 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.