Why does a JPEG file lose quality

Does simply opening and closing a JPEG file degrade the image quality?

JPEG compression can be described as having two distinct phases: first a lossy phase, then a lossless phase. For this question, it is important to understand the difference between them. This is not so much because it helps understand what is going on, but because it helps understand where the most common mistakes are coming from.

Lossy compression occurs only on, when the file is saved . This is the part that leads to loss of quality. Closing the file is not enough, however, to trigger lossy compression: you need to save it . Some editors may refuse to save unedited JPEG files to avoid accidentally triggering lossless compression. However, I am not sure if this is actually done by editors or not.

Lossless compression also occurs only on, when the file is saved . The main difference is that even if the file was closed without saving, it doesn't matter because it is lossless. JPEG uses both techniques together.

Lossless decompression occurs whenever the file is opened, but not at any other time . Not when it is closed and not even when it is saved. As with lossless decompression, it wouldn't matter if it happened during these times either because it is lossless.

"Loss of decompression" never occurs.Something like that does not exist . This may not be possible because the data that was discarded during the lossy compression phase no longer exists. If you could somehow reconstruct it, you would have a lossless compression algorithm, not a lossy one. I only mention the concept because after mentioning two types of compression it would look strange if I mentioned just one type of decompression without explaining why.

Note that saving the file will trigger both types of compression . There's not much getting around it unless you know the image hasn't been edited, but it doesn't make much sense to save it either. Also note that just closing the file without saving does not trigger either phase , not even "safe" lossless compression. Because of this, opening and closing the file cannot affect the quality of the image .


On "no point to save" I was wondering what a program would do if I only edit the metadata (comment, notes) when the user interface is open-edit-save and there is no specific indication that the image data is copied but not recompressed.

David Richerby

"Lossy Decompression" as a concept makes perfect sense, although I don't know anything about what it implements. The compressed file contains a certain amount of data. One could think of a decompression algorithm that extracts only a low resolution version of this data. This could be much faster than extracting all of the available data and one could use such an algorithm to provide a preview, for example. (For example, JPEG compresses an image as a series of 8 x 8 blocks. You can extract the average color of each block and render it as a single pixel for a 1/8 scale preview.)


@jdlugosz Programs that generally only update metadata do not decompress / compress the image part of the JPG again. You just copy this part as it is in the new file. The JPG file format is built in such a way that it is actually easier and less laborious to do so if all you need to do is update the metadata. But it all depends on the software. If that's stupid in the way it does things, there's no getting around it (except with other software).


@DavidRicherby There are many software programs out there that use the decompression engine for previews and the like. Most web-based photo albums do this by creating thumbnails and sending a low-resolution version to a browser with a small display. In MacOSX, this is done by the thumbnails in the Finder and in iPhoto. I suspect thumbnails in Explorer on Windows do the same, but I'm not sure.

Ilmari Karonen

In fact, AIUI can do both JPEG compression as well as the Decompression, be (somewhat) lossy. That is, you cannot exactly reconstruct the original image from the compressed data, and You cannot (always) exactly reconstruct the compressed data from the decompressed image. Most of the time, AFAIK, this can happen due to clipping: when the compressed YUV-DCT coefficients are converted back to 8-bit RGB pixel colors, some pixels may get color values ​​less than 0 or more than 255 which are clipped.