How to compress image using python

So here we will see how we can compress our image using python ,for this i am using pillow library and also tkinter for selecting image from system ,

So now lets understand the code i have divided my code in 3 parts

Lets see the step number 1:

Here first we need to install pillow library in our system so for that we have to open terminal and type the below command

pip install pillow

Now after that we will create one python file and start writing our code

import PIL

from  PIL import Image

from tkinter.filedialog import *

So here i have imported pillow and tkinter in our projects 

Now we will see the step number second where will take image file from our system

Step no.2

file_path = askopenfilename()

So here we have taken one variable file_path and called one function which will help us to select any particular file from our system

Now in the last step we will get the information regarding selected image and resize that image and save it into our system

Step no 3:

Image =

height, width = Image.size

Img = Image.resize((height,width), PIL.Image.ANTIALIAS)

save_path = asksaveasfilename()”Img.jpeg”)

Here in the first line we have passed the file_path and in next line get the height and width of that image and compressed that image and save that image file

Full code

I hope you have understand each line of this project.

Leave a Reply

Your email address will not be published. Required fields are marked *