How To Reduce Image Size Using Python

In this tutorial I will show you how to reduce image size using Python program. I am using Pillow package

for reducing the size of the image.

You can reduce the size of image in terms of width and height as well as size of the image in weight (for example, from 100 kb to 30 kb). So, you are basically going to compress the image from both sides.

While reducing the size of an image, you also need to maintain the quality of the image.

I am also converting the images to webp images and converting to the webp images have significantly reduced the sizes of the images.

Why do you need to reduce the size?

You might have faced an issue while uploading your image or photo to government portal for KYC purpose or any other purpose where you need to upload your or your family members photos along with other personal details. In such case, your photo size exceeds the allowed size. Let’s say your photo size is 145 KB while allowed size is 100 KB in the portal.

Online tools may not suite your requirements?

There are many online tools/software available to reduce image size, but you may not trust them. Because, these tools may save your uploaded image and you are not sure where your image gets saved in the third party software. So, you want to build your own tool to reduce the size of the image.

Prerequisites

Python 3.12, Pillow 10.4.0

Project Directory

Create a project root directory called python-reduce-image-size as per your chosen location. I am also creating other folders where optimized and scaled images will be stored from original images. These folders are – original_images, scaled_images, optimized_images & webp_images and created inside the project’s root folder.

I may not mention the project’s root directory name in the subsequent sections, but I will assume that I am creating files with respect to the project’s root directory.

Reduce Image Size

Now I am going to create a Python script which will reduce the size of the image significantly. The following Python code is written into reduce_image_size.py script file.

import os, sys
from PIL import Image

orig_img_dir = "original_images/"
dirs = os.listdir(orig_img_dir)

print('Image Size Reduction Started...')

for img in dirs:
    if os.path.isfile(orig_img_dir + img):
        im = Image.open(orig_img_dir + img)
        img_name = os.path.basename(orig_img_dir + img)
        width, height = im.size
        
        # Resize the image
        new_width = int(width / 2)
        new_height = int(height / 2)
        resized_img = im.resize((new_width, new_height), Image.LANCZOS)
        resized_img.save('scaled_images/' + img_name)
        
        # Compress the image
        im.save('optimized_images/' + img_name, quality=85, optimize=True)
        
        # Convert to WEBP
        img_name_without_extension = os.path.splitext(os.path.basename(img_name))[0]
        im.save('webp_images/' + img_name_without_extension + '.webp', format="WEBP")

print('Image Size Reduction Done.')

In the above program, I have imported the required modules from Python and Pillow packages.

I have kept the images into original_images folder under the same directory where my reduce_image_size.py script is kept.

The scaled_images folder contains the reduced images which are resized without passing optimize and quality parameters. The ANTIALIAS filter produces image with highest quality.

The optimized_images folder contains the reduced images which are resized with optimize and quality parameters. The optimize flag will do an extra pass on the image to find a way to reduce its size as much as possible. The default quality is 75, the quality value 85 will give you the optimized image with smaller size and quality of the image does not get affected much. The quality with 95 will produce image with much bigger size than quality with 85.

The webp_images folder contains the webp converted images that have very little sizes of the images compared to their png or jp(e)g counterparts.

Testing the Image Quality after reducing size

Original Images

Here are the following original images downloaded from links – https://file-examples.com/index.php/sample-images-download/ and https://sample-videos.com/download-sample-jpg-image.php for testing purpose.

You can download the images and source code later from this tutorial in the Source Code section.

file_example_JPG_2500kB.jpg – 2.38 mb

file_example_PNG_3MB.png – 2.82 mb

SampleJPGImage_20mbmb.jpg – 20.3 mb

Scaled Images

After reducing the image size the scaled image size becomes as given below:

file_example_JPG_2500kB.jpg – 1.03 mb

file_example_PNG_3MB.png – 2.98 mb

SampleJPGImage_20mbmb.jpg – 9.82 mb

Optimized Images

After reducing the image size the optimized image size becomes as given below:

file_example_JPG_2500kB.jpg – 1.69 mb

file_example_PNG_3MB.png – 2.82 mb

SampleJPGImage_20mbmb.jpg – 12.2 mb

WEBP Images

After converting to WEBP images, the sizes of the images have significantly reduced:

file_example_JPG_2500kB.jpg – 453 kb

file_example_PNG_3MB.png – 58.7 kb

SampleJPGImage_20mbmb.jpg – 9.75 mb

If you look at the reduced image size, you can find that one of the images got bigger size than original size. Therefore, you can tweak the parameters to reduce the size of the image. But the conversion to webp images has significantly reduced the size of the image.

Source Code

Download

Share

Related posts

No comments

Leave a comment