#!/bin/bash # Default heights to scale images to DEFAULT_HEIGHTS=(35 70 140 280) # Function to resize image while maintaining aspect ratio resize_image() { local input_file="$1" local height="$2" local filename=$(basename "$input_file") local dirname=$(dirname "$input_file") local extension="${filename##*.}" local name="${filename%.*}" echo "$dirname" | grep -q "mutations" && return 1 # Create the derivatives directory if it doesn't exist local output_dir="${dirname}/mutations/${name}" mkdir -p "$output_dir" local output_file="${output_dir}/${name}_${height}.${extension}" # Check if the output file already exists if [[ ! -f "$output_file" ]]; then # Use convert (ImageMagick) to resize the image convert "$input_file" -resize "x${height}" "$output_file" echo "Resized $input_file to $output_file" else echo "Skipped $input_file, $output_file already exists" fi } # Function to process directory recursively process_directory() { local dir="$1" local heights=("${!2}") find "$dir" -type d -name "*_derivatives" -prune -o -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.bmp' \) -print | while read -r file; do for height in "${heights[@]}"; do resize_image "$file" "$height" done done } # Main function main() { local target_dir="$1" shift local heights=("$@") if [[ -z "$target_dir" ]]; then echo "Usage: $0 [heights...]" exit 1 fi if [[ ${#heights[@]} -eq 0 ]]; then heights=("${DEFAULT_HEIGHTS[@]}") fi process_directory "$target_dir" heights[@] } # Call the main function with all script arguments main "$@"