Pythonの画像処理ライブラリPillowを使って画像を圧縮する。 サンプル画像はOpenCVのリポジトリにある https://github.com/opencv/opencv/blob/master/samples/data/lena.jpg を使う。 圧縮には以下のコードを用いる。

from PIL import Image

original = Image.open('img/original.jpg')

for quality in range(10, 100, 10):
    filename = 'img/{}.jpg'.format(quality)
    original.save(filename, 'JPEG', quality=quality)

Original

https://res.cloudinary.com/symdon/image/upload/v1642273476/blog.symdon.info/1642256780/original_atmqnq.jpg
Original

Quality=10

https://res.cloudinary.com/symdon/image/upload/v1642273476/blog.symdon.info/1642256780/10_kyc29h.jpg
Quality=10

Quality=20

https://res.cloudinary.com/symdon/image/upload/v1642273476/blog.symdon.info/1642256780/20_g2va2e.jpg
Quality=20

Quality=30

https://res.cloudinary.com/symdon/image/upload/v1642273476/blog.symdon.info/1642256780/30_txz42w.jpg
Quality=30

Quality=40

https://res.cloudinary.com/symdon/image/upload/v1642273477/blog.symdon.info/1642256780/40_q4abxx.jpg
Quality=40

Quality=50

https://res.cloudinary.com/symdon/image/upload/v1642273477/blog.symdon.info/1642256780/50_tzfbf8.jpg
Quality=50

Quality=60

https://res.cloudinary.com/symdon/image/upload/v1642273476/blog.symdon.info/1642256780/60_vfcm89.jpg
Quality=60

Quality=70

https://res.cloudinary.com/symdon/image/upload/v1642273476/blog.symdon.info/1642256780/70_uo3hur.jpg
Quality=70

Quality=80

https://res.cloudinary.com/symdon/image/upload/v1642273477/blog.symdon.info/1642256780/80_dqcfjo.jpg
Quality=80

Quality=90

https://res.cloudinary.com/symdon/image/upload/v1642273477/blog.symdon.info/1642256780/90_guza2u.jpg
Quality=90