Skip to content

Instantly share code, notes, and snippets.

@samithaj
Created May 1, 2017 18:07
Show Gist options
  • Save samithaj/85b9588fb80040b381f8a9411d8e5f86 to your computer and use it in GitHub Desktop.
Save samithaj/85b9588fb80040b381f8a9411d8e5f86 to your computer and use it in GitHub Desktop.
resizes all the images it finds in a folder and its subfolders
#!/bin/bash
# This script resizes all the images it finds in a folder (and its subfolders) and resizes them
# The resized image is placed in the /resized folder which will reside in the same directory as the image
#
# Usage: > ./batch_resize.sh
initial_folder="./" # You can use "." to target the folder in which you are running the script for example
resized_folder_name="./resized"
all_images=$(find -E $initial_folder -iregex ".*\.(jpg|gif|png|jpeg)")
while read -r image_full_path; do
filename=$(basename "$image_full_path");
source_folder=$(dirname "$image_full_path");
destination_folder=$source_folder"/"$resized_folder_name"/";
destination_full_path=$destination_folder$filename;
if [ ! -z "$image_full_path" -a "$image_full_path" != " " ] &&
# Do not resize images inside a folder that was already resized
[ "$(basename "$source_folder")" != "$resized_folder_name" ]; then
mkdir "$destination_folder";
# If you are not using this in an OSX system, you can use imagemagick's "convert" command instead (http://www.imagemagick.org/script/convert.php)
sips -z 299 299 "$image_full_path" --out "$destination_full_path";
fi
done <<< "$all_images"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment