To compress an image in python, you can reduce its width and height or reduce its quality. In this tutorial, we will introduce how to compress images by reducing image quality wihout width and height reduce.
If you are interesting to compress images by php, you can read this tutorial.
Import python PIL library
from PIL import Image
Open an image by PIL library
file_old= 'E:\\image-alpha.png' file_new = 'E:\\image-alpha-new.png' im = Image.open(file_old)
Then you can save this image with different quality.
im.save(file_new, quality=10,optimize=True)
The value of quality shoud be 1 (worst) to 95 (best). Above 95 shoud avoid.
We evaluate the effect.
Original image size: 97.4 KB
Compressed image size: 91.3 KB