|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -# Check if the user has provided input and output directory arguments |
| 3 | +# Function to display usage |
| 4 | +usage() { |
| 5 | + echo "Usage: $0 [-h] [-r] [-q quality] <input_directory> <output_directory>" |
| 6 | + echo "Options:" |
| 7 | + echo " -h Show this help message" |
| 8 | + echo " -r Process directories recursively" |
| 9 | + echo " -q quality WebP quality (0-100, default: 75)" |
| 10 | + exit 1 |
| 11 | +} |
| 12 | + |
| 13 | +# Default values |
| 14 | +RECURSIVE=false |
| 15 | +QUALITY=75 |
| 16 | +TOTAL_FILES=0 |
| 17 | +PROCESSED_FILES=0 |
| 18 | + |
| 19 | +# Parse arguments |
| 20 | +while getopts "hrq:" opt; do |
| 21 | + case $opt in |
| 22 | + h) |
| 23 | + usage |
| 24 | + ;; |
| 25 | + r) |
| 26 | + RECURSIVE=true |
| 27 | + ;; |
| 28 | + q) |
| 29 | + if ! [[ "$OPTARG" =~ ^[0-9]+$ ]] || [ "$OPTARG" -lt 0 ] || [ "$OPTARG" -gt 100 ]; then |
| 30 | + echo "Error: Quality must be a number between 0 and 100" |
| 31 | + exit 1 |
| 32 | + fi |
| 33 | + QUALITY=$OPTARG |
| 34 | + ;; |
| 35 | + \?) |
| 36 | + usage |
| 37 | + ;; |
| 38 | + esac |
| 39 | +done |
| 40 | + |
| 41 | +shift $((OPTIND-1)) |
| 42 | + |
| 43 | +# Check for required arguments |
4 | 44 | if [ "$#" -ne 2 ]; then |
5 | | - echo "Usage: $0 <input_directory> <output_directory>" |
6 | | - exit 1 |
| 45 | + echo "Error: Missing required arguments" |
| 46 | + usage |
7 | 47 | fi |
8 | 48 |
|
9 | | -INPUT_DIR=$1 |
10 | | -OUTPUT_DIR=$2 |
| 49 | +INPUT_DIR="$1" |
| 50 | +OUTPUT_DIR="$2" |
| 51 | + |
| 52 | +# Validate directories |
| 53 | +if [ ! -d "$INPUT_DIR" ]; then |
| 54 | + echo "Error: Input directory '$INPUT_DIR' does not exist" |
| 55 | + exit 1 |
| 56 | +fi |
| 57 | + |
| 58 | +# Create output directory if it doesn't exist |
| 59 | +mkdir -p "$OUTPUT_DIR" |
11 | 60 |
|
12 | 61 | # Check if ImageMagick is installed |
13 | | -if ! command -v magick &> /dev/null |
14 | | -then |
15 | | - echo "ImageMagick is not installed. Please install ImageMagick to proceed." |
| 62 | +if ! command -v magick &> /dev/null; then |
| 63 | + echo "Error: ImageMagick is not installed. Please install ImageMagick to proceed." |
16 | 64 | exit 1 |
17 | 65 | fi |
18 | 66 |
|
19 | | -# Process each image in the input directory |
20 | | -for input_image in "$INPUT_DIR"/*; do |
21 | | - if [[ -f "$input_image" ]]; then |
22 | | - filename=$(basename -- "$input_image") |
23 | | - output_image="$OUTPUT_DIR/${filename%.*}.webp" |
| 67 | +# Function to show progress |
| 68 | +show_progress() { |
| 69 | + local percent=$((100 * PROCESSED_FILES / TOTAL_FILES)) |
| 70 | + printf "\rProgress: [%-50s] %d%% (%d/%d files)" \ |
| 71 | + "$(printf '#%.0s' $(seq 1 $((percent/2))))" \ |
| 72 | + "$percent" \ |
| 73 | + "$PROCESSED_FILES" \ |
| 74 | + "$TOTAL_FILES" |
| 75 | +} |
| 76 | + |
| 77 | +# Function to process images |
| 78 | +process_image() { |
| 79 | + local input_file="$1" |
| 80 | + local rel_path="${input_file#$INPUT_DIR/}" |
| 81 | + local output_subdir="$OUTPUT_DIR/$(dirname "$rel_path")" |
| 82 | + local output_file="$output_subdir/$(basename "${input_file%.*}").webp" |
24 | 83 |
|
25 | | - # Use ImageMagick to convert to WebP |
26 | | - magick "$input_image" "$output_image" |
| 84 | + # Create output subdirectory if needed |
| 85 | + mkdir -p "$output_subdir" |
27 | 86 |
|
28 | | - if [ $? -eq 0 ]; then |
29 | | - echo "Image converted to WebP successfully: $output_image" |
| 87 | + # Convert image |
| 88 | + if magick "$input_file" -quality "$QUALITY" "$output_file" 2>/dev/null; then |
| 89 | + ((PROCESSED_FILES++)) |
| 90 | + show_progress |
30 | 91 | else |
31 | | - echo "An error occurred during processing: $input_image" |
| 92 | + echo -e "\nError processing: $input_file" |
32 | 93 | fi |
33 | | - fi |
34 | | -done |
| 94 | +} |
| 95 | + |
| 96 | +# Count total files |
| 97 | +if [ "$RECURSIVE" = true ]; then |
| 98 | + TOTAL_FILES=$(find "$INPUT_DIR" -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" \) | wc -l) |
| 99 | +else |
| 100 | + TOTAL_FILES=$(ls -1 "$INPUT_DIR"/*.{jpg,jpeg,png,gif} 2>/dev/null | wc -l) |
| 101 | +fi |
| 102 | + |
| 103 | +if [ "$TOTAL_FILES" -eq 0 ]; then |
| 104 | + echo "No image files found in the input directory" |
| 105 | + exit 1 |
| 106 | +fi |
| 107 | + |
| 108 | +echo "Converting $TOTAL_FILES files to WebP format (quality: $QUALITY)" |
| 109 | +echo "From: $INPUT_DIR" |
| 110 | +echo "To: $OUTPUT_DIR" |
| 111 | +echo |
| 112 | + |
| 113 | +# Process images |
| 114 | +if [ "$RECURSIVE" = true ]; then |
| 115 | + find "$INPUT_DIR" -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" \) -print0 | |
| 116 | + while IFS= read -r -d '' file; do |
| 117 | + process_image "$file" |
| 118 | + done |
| 119 | +else |
| 120 | + for ext in jpg jpeg png gif; do |
| 121 | + for file in "$INPUT_DIR"/*."$ext" "$INPUT_DIR"/*."${ext^^}"; do |
| 122 | + if [ -f "$file" ]; then |
| 123 | + process_image "$file" |
| 124 | + fi |
| 125 | + done |
| 126 | + done |
| 127 | +fi |
| 128 | + |
| 129 | +echo -e "\nConversion completed: $PROCESSED_FILES files processed" |
| 130 | +exit 0 |
0 commit comments