August 24, 2021

Saving JPEG 1 million times

One million is a nice round number and JPEG is lossy image compression. Do you have ever wondered what JPEG picture would look like if it gets saved 1,000,000 times?

Quality 90 is pretty good, right? 🔗

1
2
3
4
5
6
7
8
tmp=$(mktemp)
convert -resize 800x -quality 100 files/macbeth.jpg "$tmp"

for i in {1..1000000}; do
    convert -quality 90 "$tmp" "$tmp"
done

mv "$tmp" files/jpeg-saving/1-million-saves-quality-90.jpg
Image saved 1 million times with quality 90

Image saved 1 million times with quality 90

Oh, we lost something. Well, it was expected with lossy compression. The file size is 188 KiB.

Quality 91 wouldn’t be much better? 🔗

1
2
3
4
5
6
7
8
tmp=$(mktemp)
convert -resize 800x -quality 100 files/macbeth.jpg "$tmp"

for i in {1..1000000}; do
    convert -quality 91 "$tmp" "$tmp"
done

mv "$tmp" files/jpeg-saving/1-million-saves-quality-91.jpg
Image saved 1 million times with quality 91

Image saved 1 million times with quality 91

That’s almost original quality. The file size is 196 KiB.

About quality setting from ImageMagick documentation:

When the quality is greater than 90, then the chroma channels are not downsampled.

Test of all qualities 🔗

In this test image is saved 100 times with every quality value between 1 and 100.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
tmp=$(mktemp)

for q in {001..100}; do
    convert -resize 800x -quality 100 files/macbeth.jpg "$tmp"

    for i in {1..100}; do
        convert -quality "$q" "$tmp" "$tmp"
    done

    mv "$tmp" "files/jpeg-saving/quality-${q}.jpg"
done
Qualities from 1 to 25

Qualities from 1 to 25

Qualities from 26 to 50

Qualities from 26 to 50

Qualities from 51 to 75

Qualities from 51 to 75

Qualities from 76 to 100

Qualities from 76 to 100

Conclusion 🔗

It’s interesting how unpericable is the quality of outcome image with different quality values. Especially amount of artifacts with quality range 85-90.

Quality 30 - file size 68 KiB

Quality 30 - file size 68 KiB

Quality 90 - file size 188 KiB

Quality 90 - file size 188 KiB