PowerShell Tutorials

How To Resize Multiple Images Within A Folder With PowerShell

Learn how to resize multiple images in a folder effortlessly using PowerShell. Automate the process and save time with this step-by-step guide.

Introduction

Resizing images within a folder is a common task, especially when dealing with large collections of photos. In this article, we will explore how to resize all images within a folder using a PowerShell script. This script will automate the process and save you time and effort. With just a few simple steps, you can resize your images to the desired dimensions. Let’s dive in!

Prerequisites

Before we begin, ensure that you have PowerShell installed on your computer. You can verify this by opening the PowerShell command prompt and typing powershell. If PowerShell launches successfully, you’re ready to proceed.

The PowerShell Script

Here is the PowerShell script that will resize all images within a specified folder:

# Explicitly load the System.Drawing assembly
Add-Type -AssemblyName System.Drawing

# Set the folder path where the images are located
$folderPath = "C:\ImageSource"

# Get all image files from the specified folder
$imageFiles = Get-ChildItem -Path $folderPath -Filter *.jpg -File

# Loop through each image file
foreach ($imageFile in $imageFiles) {
    # Load the image using .NET framework
    $image = [System.Drawing.Image]::FromFile($imageFile.FullName)

    # Create a new bitmap with the desired size
    $resizedImage = New-Object System.Drawing.Bitmap(1080, 1080)

    # Create a graphics object from the new bitmap
    $graphics = [System.Drawing.Graphics]::FromImage($resizedImage)

    # Set the interpolation mode for better resizing quality
    $graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic

    # Draw the original image onto the new bitmap with resizing
    $graphics.DrawImage($image, 0, 0, 1080, 1080)

    # Dispose the objects to free up resources
    $graphics.Dispose()
    $image.Dispose()

    # Save the resized image in the same file format
    $resizedImagePath = Join-Path -Path $folderPath -ChildPath ($imageFile.BaseName + "_resized" + $imageFile.Extension)
    $resizedImage.Save($resizedImagePath)

    # Dispose the resized image object
    $resizedImage.Dispose()

    # Replace the original image with the resized image
    Move-Item -Path $resizedImagePath -Destination $imageFile.FullName -Force

    # Output the resized image path
    Write-Output "Image replaced and resized: $imageFile"
}

Step-by-Step Guide

Follow these steps to resize all images within a folder using the provided PowerShell script:

  1. Set the folderPath variable at the beginning of the script to the path where your images are located. For example: $folderPath = "C:\ImageSource"
  2. Ensure that all the images you want to resize are stored within the specified folder.
  3. Open PowerShell and navigate to the directory where the script is saved.
  4. Execute the script by running the command .\ResizeImages.ps1, replacing ResizeImages.ps1 with the name you saved the script as.
  5. The script will loop through each image file in the folder, load it, create a new bitmap with the desired size (1080×1080 in this example), and draw the original image onto the new bitmap with resizing.
  6. The resized image will be saved with the “_resized” suffix added to the original file name, maintaining the same file format.
  7. The original image will be replaced with the resized image.
  8. The script will output the path of each resized image.

Frequently Asked Questions

Q1: Can I resize images to different dimensions using the provided script?

Yes, you can modify the script to resize images to different dimensions. Simply change the parameters passed to the New-Object System.Drawing.Bitmap() and DrawImage() methods to the desired width and height values. For example, to resize images to 800×600 pixels, use $resizedImage = New-Object System.Drawing.Bitmap(800, 600) and $graphics.DrawImage($image, 0, 0, 800, 600).

Q2: Can I specify a different file format for the resized images?

Yes, the script preserves the original file format of the images. If you want to save the resized images in a different format, you can modify the Save() method’s parameters. Simply change the file extension in the $resizedImagePath variable to the desired format. For example, to save the resized images as PNG files, use $resizedImagePath = Join-Path -Path $folderPath -ChildPath ($imageFile.BaseName + "_resized.png").

Q3: Will the script overwrite the original images?

No, the script creates new files with the resized images and appends “_resized” to their file names. The original images are replaced with the resized images, ensuring you have both versions available.

Q4: Can I resize images in a subfolder within the main folder?

Yes, the script will resize images within the specified folder and its subfolders. It recursively searches for image files with the “.jpg” extension. You can modify the -Filter parameter in the Get-ChildItem command if you want to resize images with a different file extension.

Q5: Can I use this script on macOS or Linux?

No, the provided PowerShell script is specific to Windows operating systems. PowerShell is primarily available on Windows. However, you can achieve similar results using other scripting languages or image manipulation libraries available for macOS or Linux platforms.

Q6: How can I undo the resizing process if needed?

Since the script replaces the original images with the resized ones, it’s essential to have a backup of the original images before running the script. If you want to undo the resizing, you can restore the original images from your backup.

Conclusion

Resizing all images within a folder using PowerShell can greatly simplify the process, especially when dealing with a large number of images. With the provided PowerShell script and the step-by-step guide in this article, you can now automate the image resizing process effortlessly. Remember to adjust the script parameters to meet your specific requirements, such as different dimensions or file formats. Enjoy the convenience and efficiency of PowerShell for resizing your images!

I hope this article was helpful! You can find more here: PowerShell Articles

author avatar
Patrick Domingues

Leave a Comment

Stay Informed

Receive instant notifications when new content is released.