Compress images using Python


In this post, we will see how to compress images using Python.

To do this we use pillow package.

Run the below command to install the package

pip install pillow

Now, use the below code to compress an image

from PIL import Image
path = "path_to_image"
image = Image.open(path)
image = image.convert('RGB')
image.save("img/comp.jpg",quality=20,optimize=True)

The above code will compress the image size by upto 80%