How to Compress Images in Python

This tutorial will teach us how to compress the given image using the Python script. Image compression is a process of reducing an image's size without degrading the image quality. There are many tools available on the internet where we need to upload and compress the image without losing the original quality. Most of them are a great option if we want to minimize our images quickly and reliably. However, we won't use any third party API to do so. We will use the Pillow library in our Python script.

Let's get started with the Python code.

Install Pillow Library

First, we install the pillow library using the pip command.

Once the library is installed, import it into our file.

Before we dive into compressing images, let's take a following function to print the file size in a user-friendly format.

Example -

Now we will write our function for compressing the images. Let's understand the following function.

Example -

Let's break down the above function and understand how it works.

In the above code, we have created a function named compress_given_image() which takes multiple arguments -

  • First, we use the image.open() function to load the image to memory. We get the size of the image using the os.path.getsize() so we can later compare this size with the newly generated file's size.
  • We use the condition if the new_size_ratio is below 1.0, then we have to perform the resizing. This number ranges from 0 to 1 and is multiplied by the width and height of the original image to come up with a lower-resolution image. This parameter is appropriate if we want to reduce the image size further. We can also set it to 0.95 or 0.9 to loosen the image size with minimal changes to the resolution.
  • If the new_size_ratio is 1, but height and width are set, we resize to new width and height values. But we need to ensure they should be below the original width and height.
  • In the following condition, we check if the to_jpg is True, and we change the original image's extension to JPEG. It will significantly reduce the image size, especially for PNG images. If we encounter OSError, we can solve this issue by converting the image format to RGB.
  • Ultimately, we use the save() method to write the optimized image, and we set optimize to True along with quality passed from the function. Then we get the size of the compressed image and compare it with the size of the original image.

Now we call the above function in the primary function and will use the argparse module to integrate it with the command line arguments. Let's understand the following code.

We make our command-line argument parser in the above code and add the parameters we discussed before. Let's run our script in command line.

Complete Script

Now run the above script in the command-line terminal.

$ python main.py house.jpg

Output:

==================================================
Image: house.jpg
To JPEG: False
Quality: 90
Resizing ratio: 1.0
==================================================
[*] The size of image: (275, 183)
[*] Size before compression: 180.94KB
 New file saved: house_compressed.jpg
Size after compression: 100.21KB
 Image size change: 47.81% of the original image size.

$ python main.py nature.jpg -j

Output:

==================================================
Image: nature.jpg
To JPEG: True
Quality: 90
Resizing ratio: 1.0
==================================================
[*] The size of image: (612, 384)
[*] Size before compression: 80.67KB
 New file saved: nature_compressed.jpg
Size after compression: 73.37KB
 Image size change: 4.45% of the original image size.

$ python main.py sample_image.png -j -q 75

Output:

==================================================
Image: sample_image.png
To JPEG: True
Quality: 75
Resizing ratio: 1.0
==================================================
[*] The size of image: (740, 935)
[*] Size before compression: 146.32KB
 New file saved: sample_image_compressed.jpg
Size after compression: 123.03KB
 Image size change: -15.92% of the original image size.

$ python main.py sample_image.png -j -q 75 -w 800 -hh 400

==================================================
Image: sample_image.png
To JPEG: True
Quality: 75
Resizing ratio: 1.0
Width: 800
Height: 400
==================================================
[*] The size of image: (740, 935)
[*] Size before compression: 146.32KB
main.py:34: DeprecationWarning: ANTIALIAS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead.
  img = img.resize((image_width, height_width), Image.ANTIALIAS)
New Image shape: (800, 400)
 New file saved: sample_image_compressed.jpg
Size after compression: 61.76KB
 Image size change: -57.79% of the original image size.

Conclusion

In this tutorial, we have explained how we can reduce the image with restoring its original quality through Python script. We can compress the image without using any third party tools. You can copy the above code and run into to your system.






Latest Courses