Basic understanding of "JPEG Quality"?

Discussions on NConvert - the command line tool for image conversion and manipulation

Moderators: XnTriq, helmut, xnview

User avatar
Peter2
XnThusiast
Posts: 1328
Joined: Thu Nov 24, 2005 3:07 pm
Location: CH

Basic understanding of "JPEG Quality"?

Post by Peter2 »

Do I understand it right: "The quality level is always relative to the current setting"

When I save a JPG file with "quality 85%" three times, it results in a final quality of "0.85*0.85*0.85 = 61%".
When I save a JPG file with "quality 100%", it results in an unmodified quality.

Right?

Best wishes from fine snow-fall!
XnViewMP 1.6.5 German, XnConvert 1.99, Win 10
cday
XnThusiast
Posts: 3973
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Basic understanding of "JPEG Quality"?

Post by cday »

Peter2 wrote: Thu Jan 14, 2021 4:33 pm Do I understand it right: "The quality level is always relative to the current setting"

When I save a JPG file with "quality 85%" three times, it results in a final quality of "0.85*0.85*0.85 = 61%".
I am confident that is not accurate...

When I save a JPG file with "quality 100%", it results in an unmodified quality.
That is either correct, or certainly correct for all practical purposes.

JPEQ compression is complex, and I certainly have only a limited understanding: there is a detailed description on the Wikipedia JPEG page if you want more detail.

The potential loss of quality when resaving a JPEG derives from use of a quanization table in the recompression of the file, quanization tables having various possible sizes. The JPEG saving option 'use estimated quality (when possible)' is designed to minimise any loss by, I think, using the table size previously used. XnTriq is, of course, the expert on all these matters...

To further complicate matters, 'quality' scales used in software products are not standardised.

Best wishes from fine snow-fall!
Best wishes from Freistaat England! :wink:
User avatar
Peter2
XnThusiast
Posts: 1328
Joined: Thu Nov 24, 2005 3:07 pm
Location: CH

Re: Basic understanding of "JPEG Quality"?

Post by Peter2 »

The idea behind it is that I use "nconvert.exe" to add text in multiple lines. Because (at the moment) there is no multiline-feature, I have to repeat "write line - save jpg - write line - save ..."
And I want to avoid the loss of quality.
XnViewMP 1.6.5 German, XnConvert 1.99, Win 10
cday
XnThusiast
Posts: 3973
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Basic understanding of "JPEG Quality"?

Post by cday »

Peter2 wrote: Fri Jan 15, 2021 12:58 pm The idea behind it is that I use "nconvert.exe" to add text in multiple lines. Because (at the moment) there is no multiline-feature, I have to repeat "write line - save jpg - write line - save ..."
And I want to avoid the loss of quality.
I think in most situations saving a JPEG multiple times is much less of an issue than many people think.

I suggest that you save at the default quality of 85, or higher if you wish using the -q option, and possibly compare the output of successively saved versions to see if you can actually see any difference in quality. If file size is an issue you could if necessary save at 100, and then make the final save at a lower value.

Ideally you would probably save using a 'use estimated quality' option, but I don't see that option in a quick look at the NConvert 7.39 Help file, and I am not sure if it has been requested, if not it would be a useful addition... :?:

Two general observations:

In my experience images with large pixel dimensions, such as scans made at high resolution, can be compressed using quite low quality values with little or no visible loss of quality.

And the JPEG format is in principle unsuited to images containing sharp edges such as lines and text characters, nonetheless for colour and grayscale images it is often the best option overall when filesize is an issue. Images that are 1-bit black and white, however, are generally much better saved as TIFFs using 'Fax' CCITT G4 compression, resulting in lossless images with much smaller filesizes.
User avatar
XnTriq
Moderator & Librarian
Posts: 6336
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Basic understanding of "JPEG Quality"?

Post by XnTriq »

cday wrote: Thu Jan 14, 2021 6:28 pmXnTriq is, of course, the expert on all these matters...
foxyshadis is our resident expert b/c she understands the math behind it :-)

Forum references regarding the JFIF/JPEG file format:
User avatar
XnTriq
Moderator & Librarian
Posts: 6336
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Basic understanding of "JPEG Quality"?

Post by XnTriq »

Wikipedia:
https://en.wikipedia.org/wiki/JPEG#JPEG_codec_example wrote:
Although a JPEG file can be encoded in various ways, most commonly it is done with JFIF encoding. The encoding process consists of several steps:
  1. The representation of the colors in the image is converted to Y' C_B C_R, consisting of one luma component (Y'), representing brightness, and two chroma components, (C_B and C_R), representing color. This step is sometimes skipped.
  2. The resolution of the chroma data is reduced, usually by a factor of 2 or 3. This reflects the fact that the eye is less sensitive to fine color details than to fine brightness details.
  3. The image is split into blocks of 8×8 pixels, and for each block, each of the Y, C_B, and C_R data undergoes the discrete cosine transform (DCT). A DCT is similar to a Fourier transform in the sense that it produces a kind of spatial frequency spectrum.
  4. The amplitudes of the frequency components are quantized. Human vision is much more sensitive to small variations in color or brightness over large areas than to the strength of high-frequency brightness variations. Therefore, the magnitudes of the high-frequency components are stored with a lower accuracy than the low-frequency components. The quality setting of the encoder (for example 50 or 95 on a scale of 0–100 in the Independent JPEG Group's library) affects to what extent the resolution of each frequency component is reduced. If an excessively low quality setting is used, the high-frequency components are discarded altogether.
  5. The resulting data for all 8×8 blocks is further compressed with a lossless algorithm, a variant of Huffman encoding.
The decoding process reverses these steps, except the quantization because it is irreversible. In the remainder of this section, the encoding and decoding processes are described in more detail.
cday
XnThusiast
Posts: 3973
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Basic understanding of "JPEG Quality"?

Post by cday »

I hope Peter2 is following this thread closely after he asked for advice! :D
User avatar
XnTriq
Moderator & Librarian
Posts: 6336
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Basic understanding of "JPEG Quality"?

Post by XnTriq »

Useful software for analyzing and editing JPEGs:
  • Independent JPEG Group
    • jpg*exe.zip = cjpeg/djpeg + rdjpgcom/wrjpgcom + jpegtran
      “Binaries for Win32 compatible systems of sample applications of the Independent JPEG Group's free JPEG software”
    • Jpegcrop
      “The Jpegcrop Windows Application was initially developed to provide a convenient interactive user interface for the new jpegtran -crop feature. It has now developed to a nearly complete user interface replacement for jpegtran, and furthermore for demonstration of the new djpeg and jpegtran -scale features.”
    • JPEG Online Laboratory
      “This tool can be regarded as Web-based Front End to the jpegtran utility for lossless JPEG transcoding from the Independent JPEG Group. This means that all transformations performed by this tool retain the original image contents, with no further quality loss! Only reversible image transformations are performed.”
  • Toke Eskildsen
    • JPEGCrops
      “JPEGCrops is a Windows program, created for preparation of a batch of images for printing. It provides lossless cropping with fixed aspects using jpegtran.”
  • AnnyStudio
    • JPEG Lossless Rotator
      “Free and simple software for turning digital photos to the correct orientation”
  • BetterJPEG
    • BetterJPEG Editor
      “BetterJPEG is a JPEG editor specifically designed to avoid recompression loss when editing and resaving JPEG images. BetterJPEG takes advantage of the fact that JPEG images consist of a number of small independent blocks and does not recompress blocks that don't have to be changed.”
    • BetterJPEG Plug-in for Photoshop
  • RealWorld Graphics
    • RealWorld Photo Editor
      “Retouch photographs and batch process images in an easy-to-use editor. Perform lossless retouching, watermarking and other operations on .jpg images.”
  • Calvin Hass
    • JPEGsnoop
      “JPEGsnoop is a free Windows application that examines and decodes the inner details of JPEG and MotionJPEG AVI files. It can also be used to analyze the source of an image to test its authenticity.”
  • Stuffware
    • Photo Studio
      “Photo Studio is a useful tool for examining and manipulating the contents of additional data stored along with your images, supporting a wide range of formats from EXIF/DCF and CIFF (as saved by many digital cameras) through JFIF to the often used Photoshop extensions.”
  • MediaChance
    • JpgQ – JPEG Quality Estimator
      “What is the quality of JPEG already saved on disk? This small free software applet will estimate the quality of the JPG image. This is also the quality you should use if you load this image again in the photo editor (such Photobrush) and then re-save as JPG once again.”
  • Colosseum Builders
    • JPEGDUMP
      “This application dumps the contents of JPEG blocks in a JFIF file.“
  • Lucian Sabo: RIOT – Radical Image Optimization Tool
    • Stand-alone application
      “A free program designed to efficiently optimize images for the Web”
    • XnView AddOn
      “Pierre-e Gougelet, the author of XnView, created an addon to use RIOT in it’s well known viewer.
      Now XnView users have RIOT integrated into their favorite viewer to be able to easily optimize pictures using RIOT.”
    • DLL version
      “The RIOT addon for XnView is accesible after installing by selecting Filter->RIOT if an image is viewed or Tools->Plugin->RIOT in the XnView file browser.”
  • Ulead Systems
    • SmartSaver
      “Ulead SmartSaver Pro takes almost any image file format – including GIF animations – and optimizes it for the Web. Simple one-click commands, side-by-side before and after previews, customizable presets, and industry-leading optimization routines make SmartSaver Pro the best optimizer on the Internet.”
  • XAT
    • Image Optimizer
      “Image Optimizer is designed to to prepare the best possible JPEG, GIF and PNG image files for the web. File size reductions of up to 50% or often much more are possible, which can considerably decrease web page download times, reduce server load, reduce bandwidth charges and save on disk space.”
  • Phil Harvey
    • ExifTool
      “A command-line application and Perl library for reading and writing EXIF, GPS, IPTC, XMP, makernotes and other meta information in image, audio and video files.”
    • ExifToolGUI for Windows
  • Jens Duttke
    • PhotoME
      “PhotoME is a powerful tool to show and edit the meta data of image files. Thanks to the well organised layout and intuitive handling, it's possible to analyse and modify Exif and IPTC-NAA data as well as analyse ICC profiles – and it's completely FREE!”
  • Matthias Wandel
    • jhead – Exif Jpeg header manipulation tool
      “Jhead is a command line driven program for manipulating the non-image parts of Exif flavour JPEG files that most digital cameras produce.”
User avatar
Peter2
XnThusiast
Posts: 1328
Joined: Thu Nov 24, 2005 3:07 pm
Location: CH

Re: Basic understanding of "JPEG Quality"?

Post by Peter2 »

cday wrote: Tue Jan 19, 2021 6:56 pm I hope Peter2 is following this thread closely after he asked for advice! :D
Yes, of course I'm following the thread.

I'm always hoping for a "yes/no" answer and I'm always afraid when XnTriq starts his IKM (incredible knowledge machine).
XnViewMP 1.6.5 German, XnConvert 1.99, Win 10
User avatar
XnTriq
Moderator & Librarian
Posts: 6336
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Basic understanding of "JPEG Quality"?

Post by XnTriq »

Peter2 wrote: Wed Jan 20, 2021 9:27 amI'm always hoping for a "yes/no" answer and I'm always afraid when XnTriq starts his IKM (incredible knowledge machine).
I'd love to give you a straightforward answer, but when it comes to JPEG, there's no one-size-fits-all solution. It's the nature of the beast :-|

Initiating yet another BLS (binge-linking spree)… :mrgreen:
http://www.impulseadventure.com/photo/jpeg-quality.html wrote:Important note about Quality Factors

It is extremely important that the reader understand that compression quality cannot truly be represented by a single value. JPEG compression quality is actually defined by a pair of quantization tables (each with an array of 64 values). Trying to make a comparison between a pair of matrices is not at all straightforward (or always possible).

So, then why are “quality” numbers listed for each camera/software source? Many programs encode their JPEG images using quantization tables that are generated by scaling the coefficients in a “standard” table that is provided in the ITU-T specification.

The IJG group has proposed a method of scaling these coefficients according to a “quality factor” scale. This is NOT a percentage! It is merely a number from 1-100 representing the scaling factor used in generating the table. A quality factor of 100 does not mean Lossless compression! Instead, it generally represents the quality factor that will generate the highest quality compressed image with the provided scaling algorithm.

That said, many software programs and some digicams are indeed using scaled versions of this standard table. If we know that a given program has used the IJG scaling method, then we can indeed make a comparison, because all numbers in the matrices will move according to the algorithm in a similar manner.

So, what about other digicams / software editors that didn't use the IJG scaling method? People always love to make comparisons, and comparing multiple 64-element matrices is not intuitive to the average person. Therefore, as an incredibly rough approximation, a calculation has been made for each source to derive the closest / approximate IJG quality factor for a given table. If the quantization tables follow the standard trend of limited compression in the low-frequency components rising to moderate compression in the high-frequency components, then the approximate quality factor may indeed give one an idea as to how the overall quality may appear.
https://www.impulseadventure.com/photo/jpeg-snoop-uses.html wrote:Identifying JPEG Quality

One of the more interesting details about a JPEG image is how the image quality is defined. The quality is defined in what is known as a quantization table, which is a series of 64 values representing different frequency components in the image. JPEGsnoop displays these tables under the marker section DQT.

Many programs use their own tables, while some base it on the example provided in the JPEG Standard Annex K. JPEGsnoop attempts to calculate an image quality rating that is based on the standard tables (compatible with cjpeg, djpeg and other tools). To help one identify whether or not the quantization table was derived from the example ones in the Annex, JPEGsnoop calculates the ratio between the Annex table and the file's table, after the heading: AnnexRatio. If all the values nearly the same, then it implies that the quantization table is simply a scaled version of the Annex table.

If the quantization tables are indeed based on the Annex table, then the quality factor (Approx quality factor) that is displayed should be accurate.
User avatar
XnTriq
Moderator & Librarian
Posts: 6336
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Basic understanding of "JPEG Quality"?

Post by XnTriq »

When I save a JPG file with "quality 100%", it results in an unmodified quality.
This is a common misconception. I had to learn the hard way that re-encoding a JPEG is always lossy.
https://www.lifewire.com/jpeg-myths-and-facts-1701548 wrote:A Quality Setting of 100 Does Not Degrade an Image at All: False

Saving an image to JPEG format always introduces some loss in quality, although the loss at a quality setting of 100 is barely detectable by the average eye. In addition, using a quality setting of 100 compared to a quality setting of 90 to 95 or so will result in a considerably higher file size relative to the degree of image loss. If your software doesn't provide a preview, try saving several copies of an image at 90, 95, and 100 quality and compare file size with image quality. Chances are there will be no distinguishable difference between the 90 and 100 images, but the difference in size could be significant. Subtle color shifting is one effect of JPEG compression – even at high-quality settings – so JPEG should be avoided in situations where precise color matching is important.
XnView's “JPEG functions are based in part on the work of the Independent JPEG group” (InfoAbout…). The IJG is maintaining and publishing libjpeg which comes with a utility called jpegtran capable of “lossless JPEG trans-coding” (vs. en-coding). With the exception of -wipe*, Pierre has inplemented these function into his software.

XnView Classic:
  • EditMetadataClean…Optimize (Huffman table)
  • ToolsJPEG lossless tranformationsRotate 90° left (cmd_JPEGRotation270)
  • ToolsJPEG lossless tranformationsRotate 90° right (cmd_JPEGRotation90)
  • ToolsJPEG lossless tranformationsRotate 180° (cmd_JPEGRotation180)
  • ToolsJPEG lossless tranformationsFlip Vertical (cmd_JPEGVerticalFlip)
  • ToolsJPEG lossless tranformationsFlip Horizontal (cmd_JPEGHorizontalFlip)
  • ToolsJPEG lossless tranformationsCrop
* http://www.sylvana.net/jpeg-bin/jpg9dexe.zipusage.txt:
A complementary lossless-wipe option is provided to discard (gray out) data inside a given image region while losslessly preserving what is outside:

Code: Select all

-wipe WxH+X+Y	Wipe (gray out) a rectangular subarea of width W, height H starting at point X,Y.
cday
XnThusiast
Posts: 3973
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Basic understanding of "JPEG Quality"?

Post by cday »

And if Use estimated original quality (when possible) is used?

0.85*0.85*0.85 = 61%?

Or close to no additional loss?
User avatar
XnTriq
Moderator & Librarian
Posts: 6336
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Basic understanding of "JPEG Quality"?

Post by XnTriq »

cday wrote: Thu Jan 21, 2021 10:32 pmAnd if Use estimated original quality (when possible) is used?

0.85*0.85*0.85 = 61%?

Or close to no additional loss?
It depends. Let's say your JPEG that was previously saved without chroma subsampling i.e. 1×1,1×1,1×1 (best quality). Re-saving/re-encoding this JPEG with a SubSampling factor of 2×2,1×1,1×1 (default) inevitably introduces additional loss in quality. As mentioned before, the Use estimated original quality if possible setting only takes the (estimated) “quality factor” (1 – 100) into account.
User avatar
XnTriq
Moderator & Librarian
Posts: 6336
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Basic understanding of "JPEG Quality"?

Post by XnTriq »

Original image<br />www.cambridgeincolour.com/tutorials/bit-depth.htm
Original image
www.cambridgeincolour.com/tutorials/bit-depth.htm
0_original.png (72.62 KiB) Viewed 8851 times

Original PNG converted with XnView to JPEG (no chroma subsampling)
Original PNG converted with XnView to JPEG (no chroma subsampling)
1_converted.jpg (94.87 KiB) Viewed 8851 times
Conversion settings
Conversion settings

Converted JPEG resaved with XnView (default chroma subsampling)
Converted JPEG resaved with XnView (default chroma subsampling)
2_resaved.jpg (55.25 KiB) Viewed 8851 times
Resave settings
Resave settings

Converted and resaved JPEGs compared with Image Comparer<br />www.softpedia.com/get/Multimedia/Graphic/Graphic-Others/Image-Comparer.shtml
Converted and resaved JPEGs compared with Image Comparer
www.softpedia.com/get/Multimedia/Graphic/Graphic-Others/Image-Comparer.shtml
3_diff.png (21.24 KiB) Viewed 8851 times
cday
XnThusiast
Posts: 3973
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Basic understanding of "JPEG Quality"?

Post by cday »

XnTriq wrote: Thu Jan 21, 2021 11:30 pm
cday wrote: Thu Jan 21, 2021 10:32 pmAnd if Use estimated original quality (when possible) is used?

0.85*0.85*0.85 = 61%?

Or close to no additional loss?
Let's say your JPEG that was previously saved without chroma subsampling i.e. 1×1,1×1,1×1 (best quality). Re-saving/re-encoding this JPEG with a SubSampling factor of 2×2,1×1,1×1 (default) inevitably introduces additional loss in quality. As mentioned before, the Use estimated original quality if possible setting only takes the (estimated) “quality factor” (1 – 100) into account.

The XnView file 'info' displays 'Subsampling', so in principle that could be read and an image resaved using Use estimated original quality (when possible) with the same subsampling, even if you say that is not implemented now?

But what, if an image were resaved with the same subsampling 10 times, would you expect the quality of the resulting image to be: 0.107, a substantial loss of quality?
Post Reply