How to Resize All Images in a Folder Using Python Code?

Estimated read time 3 min read

To resize all images in a folder using Python, you can use the os and PIL libraries. The os library provides functions for interacting with the operating system, while the PIL library allows for image processing operations. Here’s an example code snippet that demonstrates how to resize all images in a folder:

import os
from PIL import Image

def resize_images_in_folder(input_folder, output_folder, new_size):
    """
    Resize all images in a folder and save them to another folder.

    :param input_folder: The path to the input folder containing the images.
    :param output_folder: The path to the output folder to save the resized images.
    :param new_size: The desired size of the resized images as a tuple (new_width, new_height).
    """
    # Create the output folder if it doesn't exist
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # Get a list of all files in the input folder
    file_list = os.listdir(input_folder)

    # Loop through each file in the input folder
    for file_name in file_list:
        # Get the full path of the file
        input_image_path = os.path.join(input_folder, file_name)

        # Check if the file is an image
        if os.path.isfile(input_image_path) and file_name.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
            # Open the image file
            with Image.open(input_image_path) as image:
                # Resize the image
                resized_image = image.resize(new_size)

                # Save the resized image to the output folder
                output_image_path = os.path.join(output_folder, file_name)
                resized_image.save(output_image_path)

# Example usage:
input_folder =

In this code, we define the resize_images_in_folder function that takes three parameters: input_folder (the path to the input folder containing the images), output_folder (the path to the output folder where the resized images will be saved), and new_size (the desired size of the resized images as a tuple of new_width and new_height).

Within the function, we first create the output folder if it doesn’t exist using os.makedirs. Next, we get a list of all files in the input folder using os.listdir. We then loop through each file in the input folder, checking if it is an image by verifying the file extension.

For each image file, we open it using Image.open from PIL. We resize the image using the resize method and save the resized image to the output folder using resized_image.save. The output file path is constructed using os.path.join to join the output folder path with the original file name.

In the example usage section, you need to specify the input_folder and output_folder with the paths to your input and output folders, respectively. Set new_size to the desired size of the resized images. The code will resize all images in the input folder and save the resized versions in the output folder.

Make sure you have the PIL library installed, and the input folder contains the images you want to resize.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply