Table of Contents
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.
Why do you need to reduce?
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.
Python 3.9.7, Pillow 8.4.0
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 and optimized_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 im = im.resize((width, height), Image.ANTIALIAS) im.save('scaled_images/' + img_name) #im.save('optimized_images/' + img_name, optimize=True, quality=95) im.save('optimized_images/' + img_name, optimize=True, quality=85) 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.
Testing the Image Quality after reducing size
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
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
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
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.