Image resize python automation script

Resizing images with python a solution comes in handy when you want to resize a lot of images. This script makes it easy to automate the resizing.

As I was creating a photo portfolio website for a friend of mine. He had all these nice photo’s but they were pretty big. To big to use on his website. You don’t want to load megabytes of data for a webpage just to show some photo’s. We could resize all the photo’s by hand, but as I don’t like doing things twice I wrote a Image resize python automation script.

Image resize script

This script works pretty easy. You have an image folder, where you can put in all the images that you want to resize. There is a done_images folder where all the images will be put when the resize is done (so the script will not resize it over and over again). And there is a resized folder where all the resized renames images are.

The script works with multiple (or just one) resize dimensions. So you can just say you want to resize the images or photos to one other size or you could resize an image or photo to multiple sizes.

As an example in the script it resizes to:
sizes = [[400, 400], [800, 600], [1024, 768]]

If you put in one image into the image folder, when running the script it will resize to :

  • 400×400
  • 800×600
  • 1024×768

This image resize python script will not only resize but when needed also rescale to the right dimensions.

For using this script you need to install the python-resize-image python package

by doing a

pip install python-resize-image

Don’t forget to create the folders:

  • done_images/
  • images/
  • resized/

Put your images to resize in the images folder and run the script!

Thats all.

'''
author: Theo van der Sluijs
url: https://itheo.tech
copyright: CC BY-NC 4.0
creation date: 01-12-2018

Script to resize images to one or multiple sizes

You need 3 folders for this script
/done_images/
/images/
/resized/

/images/
in this folder you put the images to resize

/done_images/
The script will backup your done images to this folder

/resized/
This is the folder where all the resized images will be put


You need to install one python package that will install PIL and resizeImage
https://pypi.org/project/python-resize-image/

pip install python-resize-image
'''
import os
from PIL import Image
from resizeimage import resizeimage


class resizer:
    def __init__(self):
        """
        start of class
        setting some vars
        """
        self.sizes = ""
        self.images = []
        self.new_file_names = []

    def loop_images(self, startfolder):
        """
        Function to loop over images to resize
        :param startfolder:
        """
        for filename in os.listdir(startfolder):
            if filename.endswith(".jpeg") or filename.endswith(".jpg"):
                self.images.append(os.path.join(startfolder, filename))

    def get_file_name(self,image):
        """
        Simple function to get file/ image name
        :param image:
        :return:
        """
        return os.path.basename(image)

    def get_file_name_only(self, image):
        """
        Simple function to get name of file without extension
        :param image:
        :return:
        """
        base = os.path.basename(image)
        return os.path.splitext(base)[0]

    def new_file_name(self, image, newfolder, size):
        """
        Function to create new file name
        :param image:
        :param newfolder:
        :param size:
        :return:
        """
        file_name = self.get_file_name_only(image)
        new_name = "{}_{}_{}.jpg".format(file_name, size[0], size[1])
        self.new_file_names.append(new_name)
        new_file = os.path.join(newfolder, new_name)
        return new_file

    def resize_image(self, image, new_file, size=[200, 200]):
        """
        The actual functin to resize the image
        :param image:
        :param new_file:
        :param size:
        """
        with open(image, 'rb') as f:
            with Image.open(f) as img:
                img = resizeimage.resize_cover(img, size)
                img.save(new_file, img.format)


if __name__ == '__main__':
    dir_path = os.path.dirname(os.path.realpath(__file__))

    # set your own image sizes here in list structure
    sizes = [[400, 400], [800, 600], [1024, 768]]

    # if you want you can set your own folder names here
    startfolder = os.path.join(dir_path, "images")
    newfolder = os.path.join(dir_path, "resized")
    done_images = os.path.join(dir_path, "done_images")

    r = resizer()
    r.loop_images(startfolder)

    #looping over images
    for image in r.images:
        for size in sizes:
            new_file = r.new_file_name(image, newfolder, size)
            r.resize_image(image, new_file, size)

        file_name = r.get_file_name(image)
        backup_file = os.path.join(done_images, file_name)
        os.rename(image, backup_file)

    print(r.new_file_names)

Did you find this article valuable?

Support Theo van der Sluijs by becoming a sponsor. Any amount is appreciated!