Created
January 4, 2025 21:36
-
-
Save abdulajet/4f9f8584dac680dbec35cd21590a3264 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Ensure a parent directory is specified | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <parent_directory>" | |
exit 1 | |
fi | |
parent_dir="$1" | |
# Check if the parent directory exists | |
if [ ! -d "$parent_dir" ]; then | |
echo "Directory not found: $parent_dir" | |
exit 1 | |
fi | |
# Initialize a queue with the parent directory | |
directories_to_process=("$parent_dir") | |
# Loop through the directories in the queue | |
while [ ${#directories_to_process[@]} -gt 0 ]; do | |
# Pop the first directory from the queue | |
input_dir="${directories_to_process[0]}" | |
directories_to_process=("${directories_to_process[@]:1}") | |
# Check if the current directory contains any img files | |
if find "$input_dir" -maxdepth 1 -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.JPG" -o -iname "*.png" \) | grep -q .; then | |
echo "Processing directory: $input_dir" | |
# Create a temporary directory for processed images | |
temp_dir="./temp_processed_images" | |
mkdir -p "$temp_dir" | |
# Preprocess images to make all landscape | |
find "$input_dir" -maxdepth 1 -type f \( -iname "*.JPG" -o -iname "*.PNG" -o -iname "*.JPEG" \) | while read -r img; do | |
orientation=$(identify -format "%[fx:w>h?0:1]" "$img") | |
if [ "$orientation" -eq 1 ]; then | |
# Rotate portrait images to landscape | |
magick "$img" -rotate -90 "$temp_dir/$(basename "$img")" | |
else | |
# Copy landscape images as is | |
cp "$img" "$temp_dir/$(basename "$img")" | |
fi | |
done | |
# Generate list of processed images | |
find $temp_dir -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.JPG" -o -iname "*.png" \) | sort > imglist | |
# Calculate number of images | |
read nImgs <<< $(sed -n '$=' imglist) | |
# Calculate rows and columns | |
let columnSize=6 | |
let nrows=nImgs/columnSize | |
let lefto=nImgs%columnSize | |
if [ $lefto -gt 0 ]; then | |
let nrows=nrows+1 | |
fi | |
# # Extract folder name and use in montage | |
output_name=$(basename "$input_dir") | |
montage @imglist -geometry 200x150+2-5 -tile ${columnSize}x${nrows} "${output_name}.jpg" | |
# # Cleanup | |
rm imglist | |
rm -rf "$temp_dir" | |
else | |
echo "No images found in directory: $input_dir, checking subdirectories." | |
# Add subdirectories of the current directory to the queue | |
for sub_dir in "$input_dir"*/; do | |
if [ -d "$sub_dir" ]; then | |
directories_to_process+=("$sub_dir") | |
fi | |
done | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment