Welcome folks today in this blog post we will be compressing
or minifying
single image in python using the pillow
library in python. All the full source code of the application is given below.
Get Started
In order to get started you need to install the following library using the pip
command as shown below
pip install pillow
After installing this library make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 |
from PIL import Image im = Image.open("profile.png") print(f"The image size dimensions are: {im.size}") # The image size dimensions are: (1920, 1280) im.save("Compressed.png",optimize=True,quality=5) |
So here in the first line of the python above script
we are importing the pillow
library and then we are reading or loading the input image
which is not compressed and then we are printing out the dimensions of the image and also lastly we are compressing the image by using the save
method of the library as shown below
Now if you execute the python
script by typing the below command
python app.py
Now you can see the difference between the non-compressed and compressed image as shown below