#!/bin/bash # ResizeImageToMatrix384.sh # Invoke this script in the same directory as the source image files. # Converts image area to 384 squared pixels or the specified argument. # Run this by typing: ./ScriptFileName.sh [matrix] # Or omit matrix size to use default size 384. # Set target image matrix size X*Y: squareMatrix=${1:-384} echo "Matrix size: $squareMatrix" # Loop through files in the current folder for file in *.png *.gif *.jpg *.webp; do test -e "$file" || continue echo 'Processing '"$file" # Determine input image dimensions using ImageMagick's identify command read inputWidth inputHeight < <(identify -format "%[fx:w] %[fx:h]" "$file") # Calculate the scaling factor for the width and height to achieve total pixels scaleFactor=$(bc <<< "scale=6; sqrt($squareMatrix*$squareMatrix/$inputWidth/$inputHeight)") echo "Scale factor: $scaleFactor" # Calculate the output dimensions and round to the nearest integer. outputWidth=$(bc <<< "scale=6; round=($inputWidth*$scaleFactor); scale=0; round/1") outputHeight=$(bc <<< "scale=6; round=0.5 + ($inputHeight*$scaleFactor); scale=0; round/1") # Resize the input image using ImageMagick's convert command convert "$file" -resize "${outputWidth}x${outputHeight}" "${file%.*}-${squareMatrix}.${file##*.}" echo done echo -en '\a' read -rs -n1 -t60 -p'Ctrl+C to exit or any key to pause!' || { echo; exit;} echo -en '\r\aPress Ctrl+C to exit permanent hold!' sleep infinity