CBSE Today

Resize Images using Python – folder wise

Resize Images using Python program available in any folder. This python program checks all the folder and subfolders available in the selected source folder and saves resized images in the given destination folder.

Image resizing is one such task that all of us has to do., reasons may very. This is one such task that as a computer Teacher/students we can not deny. But if the number of images comes to you in thousands? And they also come in sub-folders? Well, this program is written exactly for this same situation.

Python PIL library is specially written to manipulate the images on the fly. PIL has several filters that are designed to manipulate images as per our requirements.

I wrote this program because I have to upload thousands of images on our website after any event. Converting each image manually was so much pain so I decided to automate this task, Automate your Boring stuff by al awagger was a great help in developing this code.

How to use Resize images using Python code.

  1. Just copy and paste the bellow Resize images using python code into your favorite IDE.
  2. Change the image size in line number 23 as per your requirement.
  3. Run this code using the run command. Make it very sure your system has Python as well as PIL and resize image libraries installed.
  4. select the Source folder – whose images you want to resize.
  5. select the destination folder – This folder will hold all resized images.

Python code for Bulk Image resizing

# Name:             small images
# Purpose:          Program to create small images
# Author:           rakesh kumar
# Created:          09-02-2018
# Copyright:        rakesh kumar
# Licence website :
import os
import glob
import sys
import re
import tkinter as tk
from glob import glob
from tkinter import filedialog
from PIL import Image
from resizeimage import resizeimage

def small(source,target,original):
    dest = target + '\\' +original
    with open(original, 'r+b') as f:
        with as image:
            cover = resizeimage.resize_cover(image, [640, 480], validate=False)
  , image.format)

def deleteFiles():
    root = tk.Tk()
    rootdir = filedialog.askdirectory()
    if not rootdir.strip() :
        print ( "Folder Error...User did not selected any folder" )
    #rootdir = 'C:\\Users\\acer\\Desktop\\HigherThinkingC++'
    targetdir = filedialog.askdirectory()
    count =0
    for root, subFolders, files in os.walk(rootdir):
         files =  glob('*.jpg')+ glob('*.jpeg') + glob('*.png')
         for filename in files:
            filename1,ext = os.path.splitext(filename)
            if(ext =='jpg' or ext=='jpeg'or ext=='png'):
                count= count+1
            #print filename

    print ( "\n\n\nJob Complete " + str(count) + "....Check your Destination folder now\n\n" )

if __name__ == '__main__':

The above image size resizer program took the help of four basic modules python libraries “os”,”glob”, ” sys”,  “re”,  “tkinter”, and  “glob”. Tkinter was used to select the source folder and destination folder so that a user could not make any error while typing.

The program right now converts JPG and PNG files but you can easily expand this program to resize other image files formats. Hope this program will help you a lot. If you have any query to suggestions, please send me via comments.

Exit mobile version