Batch Resize Images in Folders Using Shell Script or ImageMagick
When dealing with a large number of images across multiple folders, performing actions such as resizing can become tedious and time-consuming. If you are working on a project or maintaining an image gallery, learning how to batch resize images within subfolders can save you a lot of time and effort. This guide will discuss how to use shell scripts or ImageMagick to resize all images inside folders within a main folder. It is important to mention the operating system you are using, as the steps may differ.
Understanding the Structure
Consider the following directory structure:
mainfolder - folder1 - folder2 - and so onEach folder contains images that need to be resized. The goal is to loop through each folder and then loop through the images within those folders, applying the desired resize operation.
Method 1: Using Bash Script and ImageMagick
Let's explore how to achieve this using a combination of Bash scripting and ImageMagick, a powerful image manipulation tool. Bash is the default shell in GNU/Linux, while ImageMagick supports various formats and provides a wide range of image processing capabilities.
Step 1: Install the Required Tools
On a GNU/Linux system or macOS, you can install ImageMagick using:
sudo apt-get install imagemagick # For Debian-based systems brew install imagemagick # For macOSIf you are using Windows, you can use Git Bash to run these commands or install the tools through Chocolatey:
choco install imagemagick # For WindowsStep 2: Writing the Bash Script
To resize all images in a main folder and its subfolders, you can use a combination of find and resize commands. Here’s an example script:
#!/bin/bash mainfolder"mainfolder" width2000 height1000 # Loop through all folders inside the main folder find "$mainfolder" -maxdepth 2 -type d | while read -r folder; do echo "Resizing images in $folder" # Loop through images in the current folder find "$folder" -type f -name "*.jpg" | while read -r image; do convert "$image" -resize ${width}x${height} "resized_$image" done doneIn this script:
mainfolder is the name of your main directory. width and height are the dimensions to which you want to resize the images. find "$mainfolder" -maxdepth 2 -type d recursively traverses the subdirectories within the main folder. find "$folder" -type f -name "*.jpg" finds all .jpg files in the current subfolder. The convert command from ImageMagick resizes the images. The resized images are saved with the prefix resized_.Method 2: Using a WebP Format with Google's WebP
Google recently introduced WebP, a new image format that provides higher compression ratios than JPEG and better performance. If you prefer to use the WebP format, you can modify the script as follows:
#!/bin/bash mainfolder"mainfolder" width2000 height1000 # Loop through all folders inside the main folder find "$mainfolder" -maxdepth 2 -type d | while read -r folder; do echo "Converting images to WebP in $folder" # Loop through images in the current folder find "$folder" -type f -name "*.jpg" | while read -r image; do cwebp -resize ${width} ${height} "$image" -o "resized_${image%.jpg}.webp" doneIn this WebP script:
The cwebp command is used to convert and resize the images to WebP format. The resized WebP images are saved with the same name as the original, but with the .webp extension.Alternative Solutions: Using Freeware Programs
For users without a programming background, there are numerous freeware programs available that can batch resize images. A simple Google search can lead you to these tools. Examples include:
FastStone Image Resizer Advanced Photo Optimizer EasyResizerThese programs often come with user-friendly interfaces and advanced features such as batch processing, image adjustments, and color correction.
Conclusion
Batch resizing images in folders can significantly improve the efficiency of your image management tasks. Whether you prefer a command-line approach using Bash and ImageMagick or opt for a freeware application, there are multiple methods to accomplish this task effectively. Choose the method that best fits your needs and workflow.