#!/bin/bash # Script to process new images from new-images/ directory # Checks for duplicates using checksums, converts to WebP, and continues numbered sequence set -e # Directory paths NEW_IMAGES_DIR="./new-images" IMAGES_DIR="./images" TEMP_CHECKSUMS="/tmp/existing_checksums.txt" TEMP_NEW_CHECKSUMS="/tmp/new_checksums.txt" # Check if directories exist if [ ! -d "$NEW_IMAGES_DIR" ]; then echo "Error: $NEW_IMAGES_DIR directory not found" exit 1 fi if [ ! -d "$IMAGES_DIR" ]; then echo "Error: $IMAGES_DIR directory not found" exit 1 fi # Check if there are any images to process if [ -z "$(ls -A $NEW_IMAGES_DIR 2>/dev/null)" ]; then echo "No images found in $NEW_IMAGES_DIR" exit 0 fi echo "Starting image processing..." # Find the highest number in the existing sequence HIGHEST_NUM=$(ls "$IMAGES_DIR"/*.webp 2>/dev/null | sed 's/.*\///' | sed 's/\.webp$//' | sort -n | tail -1) if [ -z "$HIGHEST_NUM" ]; then HIGHEST_NUM=0 fi echo "Highest existing number: $HIGHEST_NUM" NEXT_NUM=$((HIGHEST_NUM + 1)) # Generate checksums for existing images echo "Generating checksums for existing images..." find "$IMAGES_DIR" -type f \( -name "*.png" -o -name "*.jpg" -o -name "*.jpeg" -o -name "*.webp" \) -exec md5sum {} \; | awk '{print $1}' | sort > "$TEMP_CHECKSUMS" # Process each new image PROCESSED=0 SKIPPED=0 for img in "$NEW_IMAGES_DIR"/*; do # Skip if not a file [ -f "$img" ] || continue # Get file extension ext="${img##*.}" ext_lower=$(echo "$ext" | tr '[:upper:]' '[:lower:]') # Only process image files if [[ ! "$ext_lower" =~ ^(jpg|jpeg|png|gif|bmp|tiff|webp)$ ]]; then echo "Skipping non-image file: $(basename "$img")" continue fi # Calculate checksum of new image NEW_CHECKSUM=$(md5sum "$img" | awk '{print $1}') # Check if checksum already exists if grep -q "^${NEW_CHECKSUM}$" "$TEMP_CHECKSUMS"; then echo "Duplicate found (checksum match): $(basename "$img") - skipping" SKIPPED=$((SKIPPED + 1)) continue fi # Convert to WebP with numbered filename OUTPUT_FILE="${IMAGES_DIR}/${NEXT_NUM}.webp" echo "Processing: $(basename "$img") -> ${NEXT_NUM}.webp" # Convert to WebP using ImageMagick or cwebp if command -v cwebp &> /dev/null; then cwebp -q 85 "$img" -o "$OUTPUT_FILE" 2>/dev/null elif command -v convert &> /dev/null; then convert "$img" -quality 85 "$OUTPUT_FILE" else echo "Error: Neither cwebp nor ImageMagick (convert) found. Please install one of them." exit 1 fi # Add new checksum to temp file to avoid processing duplicates in same run echo "$NEW_CHECKSUM" >> "$TEMP_CHECKSUMS" PROCESSED=$((PROCESSED + 1)) NEXT_NUM=$((NEXT_NUM + 1)) done # Cleanup rm -f "$TEMP_CHECKSUMS" "$TEMP_NEW_CHECKSUMS" echo "" echo "Processing complete!" echo "Images processed: $PROCESSED" echo "Images skipped (duplicates): $SKIPPED" echo "Next available number: $NEXT_NUM"