Resize Images using Python

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.

resize images using python
resize images in bulk using python code

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 : https://www.cbsetoday.com
#-------------------------------------------------------------------------------
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 Image.open(f) as image:
            cover = resizeimage.resize_cover(image, [640, 480], validate=False)
            cover.save(dest, image.format)

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

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

if __name__ == '__main__':
    deleteFiles()

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.

Leave a Reply

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