How to resize image using Python

Python library Pillow can be used to resize images. Please note resize does not mean compressing image. Yes, reducing pixels can lead to reduction in file size in terms of KBs however it will not be significant. If you need to compress image, please check How to compress image using python.

While resizing the image file, you need to maintain the aspect ration, else image might get distorted, we will see an example of the same.

Here is sample program to resize.

here is the output

$ python3.6 04_resize_image_01.py 
*** Program Started ***
im.size (1920, 1318)
im.size (960, 659)
*** Program Ended ***

Here we have maintained the aspect ratio so the files will look similar
Input

Output

Now lets look at resize by not maintaining the aspect ratio

Output of the program

$ python3.6 04_resize_image_02.py 
*** Program Started ***
im.size (1920, 1318)
im.size (500, 480)
*** Program Ended ***

You can see the issue with changing of aspect ratio.

Input

Output

Size comparison 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.