Kickstart your coding journey with our Python Code Assistant. An AI-powered assistant that's always ready to help. Don't miss out!
Image compression is the process of minimizing the size of an image without degrading the image quality. There are a lot of tools online that offer this service; most of them are a great option if you want to minimize your images quickly and reliably. However, in this tutorial, you will learn to reduce image file size in Python using the Pillow library.
You're free to use the code for this tutorial. For instance, you can make an API around it to reduce image sizes in batches instead of using a third-party API that may cost you money.
I made the code for this tutorial as flexible as possible. You can compress the image and resize it with a scaling factor or exact width and height. You can also specify the quality ratio.
Alright, to get started, let's install Pillow:
Open up a new Python file and import it:
Before we dive into compressing images, let's grab a function from this tutorial to print the file size in a friendly format:
Next, let's make our core function for compressing images:
A giant function that does a lot of stuff, let's cover it in more detail:
Image.open()
method to load the image to the memory, we get the size of the image file using os.path.getsize()
so we can compare this size later with the newly generated file's size.new_size_ratio
is set below 1.0
, then resizing is necessary. 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 is a suitable parameter if you want to reduce the image size further. You can also set it to 0.95
or 0.9
to reduce the image size with minimal changes to the resolution.new_size_ratio
is 1.0
, but width
and height
are set, then we resize to these new width
and height
values, make sure they're below the original width
and height
.to_jpg
is set to True
, we change the extension of the original image to be JPEG. This will significantly reduce image size, especially for PNG images. If the conversion raises an OSError
, converting the image format to RGB will solve the issue.save()
method to write the optimized image, and we set optimize
to True
along with the quality passed from the function. We then get the size of the new image and compare it with the size of the original image.Now that we have our core function, let's use argparse
module to integrate it with the command-line arguments:
We make our command-line argument parser in the above code and add the parameters we discussed before.
Let's use our script now. You can get the example image here. First, let's use our script without any parameters:
Output:
The image size is reduced to 379.25KB from 425.65KB, which is about an 11% reduction. Next, let's try to pass -j
for converting from PNG to JPEG:
Output:
Note: You can get the example image here.
That's fantastic, 76.5% of improvement. Let's decrease the quality a bit:
Output:
About 85% of reduction without touching the original image resolution. Let's try to multiply the width
and height
of the image by 0.9
:
Output:
Now setting exact width
and height
values:
Output:
Awesome! You can try to tweak the parameters to fit your specific needs. I hope this script was handy for you to accelerate the development of your application.
Check the complete code here.
Learn also: How to Compress PDF Files in Python
Happy coding ♥
Want to code smarter? Our Python Code Assistant is waiting to help you. Try it now!
View Full Code Analyze My Code
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!