Bulk Image Resizing – PowerShell v3 Script for Windows
Bulk Photo Resizing – PowerShell v3 Script for Windows
Introduction
Many times, our clients find it difficult to resize images so they can upload them correctly to Magento products. Sometimes their suppliers provide either very large or inconsistent photos, which makes it difficult for them to achieve a good result. Usually, we can ask Magento to resize images before displaying them to visitors, however we believe it is preferable for images to be uploaded in the correct size from the outset.
For this reason, we decided to write a small PowerShell script that resizes all images in a folder to 1000 x 1000 px. This is a reasonable dimension for product images on a website in most cases, unless very high resolution is required for zooming in to showcase textures and fine details.
Requirements
For this script to work, the following prerequisites must be installed, if they are not already present on your system:
PowerShell v3+: If you are using Windows 10, you already have PowerShell version v3 installed. If you are using an older version of Windows, you may need to upgrade it from the Microsoft website.
ImageMagick library: The script uses the ImageMagick library to resize images, so it must be installed on your system. Make sure to download the correct version of the library according to your Windows architecture, 32-bit or 64-bit, from the official website.
The script does not require any special installation. Simply create a file named resizer.ps1 and paste the following code into it. Next, copy it to the folder containing the images you want to resize.
#
# Script.ps1
#
$scriptPath=$PSScriptRoot
#$scriptPath=""
cd $scriptPath
Remove-Item -Recurse -Force "resized"
mkdir "resized"
Write-Host $scriptPath
$files = Get-ChildItem $scriptPath -Filter *.jpg
foreach ($file in $files) {
$oldPath=$file.FullName
$baseName=$file.BaseName
$ext=$file.Extension
$newPath="$scriptPath\resized\$baseName-1000x1000$ext"
#Copy-Item $oldPath $newPath
magick "$oldPath" -resize 1000x1000 -background white -gravity center -extent 1000x1000 "$newPath"
}
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Usage Instructions
Copy the script resize.ps1 into the folder containing the images you want to resize. Right-click the file resize.ps1 and select Run with PowerShell. The script will automatically resize all the photos in that folder and place them in a subfolder named resized. The script is non-destructive, and your original photos will remain untouched.
Adjust the dimensions of the output image.
By default, the script will resize the image canvas to 1000×1000 px and add a white background to the canvas. If you wish, you can change the dimensions by editing the script using Notepad.
and on line 19:
magick "$oldPath" -resize 1000x1000 -background white -gravity center -extent 1000x1000 "$newPath"
change “1000×1000” to the desired dimensions. If your images have a black background, you can also change the parameter
“-background white”
In:
“-background black”
to add a black background to the canvas instead of white.
When you visit any web site, it may store or retrieve information on your browser, mostly in the form of cookies. Control your personal Cookie Services here.