Python 3 Pillow Library Script to Compress or Minify Single PNG or JPEG Image to Small Size Full Project For Beginners

Python 3 Pillow Library Script to Compress or Minify Single PNG or JPEG Image to Small Size Full Project For Beginners

 

Welcome folks today in this blog post we will be compressing or minifying single image in python using the pillowlibrary 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

 

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

See also  Python 3 Script to Find Factorial of a Number Full Tutorial For Beginners

 

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

 

 

 

 

Leave a Reply