How to Compress Images in PythonThis 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 LibraryFirst, 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 -
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 ScriptNow 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. ConclusionIn 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. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India