Does re-saving a JPEG keep making it worse? At quality 75 mine stopped changing at generation 15
"Never re-save a JPEG, every save makes it worse" was my working rule for image formats. The picture behind it was a photocopy of a photocopy, each generation a bit blurrier and a bit smaller. Then came a test: one photo
"Never re-save a JPEG, every save makes it worse" was my working rule for image formats. The picture behind it was a photocopy of a photocopy, each generation a bit blurrier and a bit smaller. Then came a test: one photo saved 20 times in a row, with every generation measured. With the same encoder and the same quality number, the photo lost almost everything it was going to lose in the first save. After that it barely moved, and then it stopped.
Almost all the loss was in the first save
The setup is small. One 1200Γ672 countryside photo (trees, a stone wall, grass), kept as a PNG to serve as the clean original. It was saved as JPEG with Pillow, which uses libjpeg-turbo underneath, with 4:2:0 chroma subsampling, then reopened and re-saved at the same quality for 20 generations. Each one was compared with the PNG by PSNR, where higher means closer to the original. It ran once, on one Apple-silicon Mac, so every number is "this photo, this run".
At quality 75 the first save landed at 29.70 dB. Generation 5 was 29.58 and generation 20 was 29.55. The next 19 saves together cost 0.15 dB. The file did not shrink either. Generation 1 was 209,453 bytes and generation 20 was 209,415, 38 bytes apart. At every quality in the test the size moved by less than 2%, so the "smaller every time" half of my old picture was simply wrong.
The file stopped changing at generation 15
What surprised me most is that the curve doesn't just flatten, it ends. From generation 15 onward the decoded pixels were identical to the previous generation, and so were the bytes. You can see it without any metric, using the libjpeg-turbo command-line tools and a checksum (md5 -q is the macOS one), with photo.ppm as the original:
$ cjpeg -quality 75 -outfile g01.jpg photo.ppm; for i in $(seq 2 20); do djpeg -ppm g$(printf %02d $((i-1))).jpg | cjpeg -quality 75 -outfile g$(printf %02d $i).jpg; done; md5 -q g01.jpg g02.jpg g14.jpg g15.jpg g16.jpg g20.jpg; ls -l g01.jpg g20.jpg | awk '{print $5, $9}'
9c8682676c38b3355fc8a6622ed471df
63cf1b8d26ed75c7e63e0f0eb3a209cc
025f8fa26b289855a0ca096bafa99258
025f8fa26b289855a0ca096bafa99258
025f8fa26b289855a0ca096bafa99258
025f8fa26b289855a0ca096bafa99258
209453 g01.jpg
209415 g20.jpg
Generations 1 and 2 differ, so the second save did change something. Generations 14, 15, 16 and 20 share one checksum: saving again produces the file you already had. My way of explaining it to myself: a JPEG save rounds the image's frequency data to a grid of allowed values, and the quality number sets how coarse the grid is. After the first save the data already sits on that grid, so a second pass has little left to round, and each further pass leaves fewer values near enough to an edge to flip. Nothing in this test confirms it, since no coefficients were dumped, so it is a working model and not a proof.
Generation 15 is not a property of JPEG, though. A second photo, a lake with smooth water and sky, went from 34.69 to 34.65 dB at quality 75 and stopped changing at generation 10.
Quality 90 and above had not settled in 20 saves
Against my guess, higher quality was worse at settling. The bridge photo at quality 90 started at 34.21 dB and was at 33.08 by generation 20, and it was still changing: 4,198 pixels differed between generations 19 and 20. At quality 95 it fell from 36.81 to 33.75, a drop of 3.06 dB against 0.15 at quality 75. By generation 20 the quality 95 chain was only 0.67 dB ahead of the quality 90 chain while its file was 36% larger. There is no tested explanation for this here, and where these chains end is unknown because the test stopped at 20. A self-made UI screenshot did worse still: at quality 90 it lost 6.13 dB over 20 generations (37.99 to 31.86) and had not settled by generation 20.
Changing the quality between saves kept the damage going
So my mental model is different now. This photo at quality 75, re-saved with identical settings, paid once and then idled. What kept the damage going was the settings moving between saves. In the same test, a chain that rotated through quality 90, 75 and 85 ended at 28.12 dB, 1.43 dB below the chain that used 75 every time, though two thirds of its saves used a higher number. My reading, again untested: a different quality means a different grid, so every save has fresh rounding to do.
The browser tool I use for one-off compression is ImgIng https://imging.ai/ , and the same check was done there with a JPEG it had exported itself: fed back in at the same setting, 316,478 bytes came out as 316,443, the page noted that the size had not gone down, and PSNR against the first pass was 52.63 dB. Little lost, nothing gained, like the Pillow chain.
One extra save at the same setting no longer worries me, and a higher quality number no longer looks like insurance for a file that gets re-saved a lot, since the high-quality chains were the ones still drifting at generation 20. For any particular image the checksum loop above answers the question: where two neighbouring generations share a checksum, that image at that quality in that encoder has nothing left to lose.
Photo sample: Wikimedia Commons, "2013 Cogden Bridge" by Kreuzschnabel, CC BY-SA 3.0, downscaled, cropped and repeatedly recompressed. The second photo is "Inle Lake, Myanmar" by Jakub HaΕun, CC BY-SA 4.0. The screenshot sample is a self-made UI.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.