Bash Script to Batch Resize all Images in a Folder

A simple script which will recognize all image types in the PWD and reduce them by 50% and copy them to the resized folder under PWD. Good for bulk resizing of images.

files=($(file -i * | awk -F ':' 'BEGIN {ORS=""} /^.*:[ \t]*image\/[a-z]*; charset=binary/ {for(i=1;i<NF-1;i++) {print $i":"} {print $(NF-1)"\n"} }' | sed 's/ /:spacecharacter:/g' | tr '\n' ' '))
echo ${files[@]}
mkdir -p resized
for file in ${files[@]}
  file=$(echo $file | sed 's/:spacecharacter:/ /')
  convert -resize 50% "$file" resized/"$file"
  #You can use mogrify instead of resize if you want to edit the images in place.
  #mogrify -resize 50% "$file"

Copy the script to ~/bin directory and make it executable chmod +x resize_images. Change into the directory containing the images and execute the script.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.