opencand.infra/jpg-normalize.sh
2025-06-07 18:49:00 +00:00

129 lines
3.5 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# OpenCand JPEG Normalization Script
# This script normalizes .jpeg/.JPEG files to .jpg extension in candidate photo directories
set -e # Exit on any error
# Configuration
PHOTOS_DIR="./fotos_cand"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Statistics counters
TOTAL_DIRS=0
TOTAL_JPEG_FILES=0
FILES_RENAMED=0
FILES_SKIPPED=0
ERRORS=0
echo -e "${YELLOW}🖼️ OpenCand JPEG Normalization Script${NC}"
echo "========================================"
# Check if photos directory exists
if [ ! -d "$PHOTOS_DIR" ]; then
echo -e "${RED}❌ Error: Photos directory '${PHOTOS_DIR}' not found${NC}"
exit 1
fi
echo -e "${YELLOW}📋 Pre-normalization checks:${NC}"
echo "✅ Photos directory exists"
# Change to photos directory
cd "$PHOTOS_DIR" || {
echo -e "${RED}❌ Error: Cannot access photos directory${NC}"
exit 1
}
echo -e "${BLUE}📂 Scanning directories for JPEG files...${NC}"
# Enable case-insensitive globbing
shopt -s nocaseglob
# First pass: count files and directories
for dir in */; do
[ -d "$dir" ] || continue
TOTAL_DIRS=$((TOTAL_DIRS + 1))
cd "$dir" || continue
for f in *.jpeg; do
[ -f "$f" ] || continue
TOTAL_JPEG_FILES=$((TOTAL_JPEG_FILES + 1))
done
cd ..
done
echo -e "${BLUE}📊 Scan results:${NC}"
echo " Directories found: $TOTAL_DIRS"
echo " JPEG files found: $TOTAL_JPEG_FILES"
echo ""
if [ $TOTAL_JPEG_FILES -eq 0 ]; then
echo -e "${GREEN}✅ No JPEG files found to normalize. All files are already in correct format!${NC}"
exit 0
fi
echo -e "${YELLOW}🔄 Starting normalization process...${NC}"
# Second pass: perform the actual renaming
for dir in */; do
[ -d "$dir" ] || continue
cd "$dir" || {
echo -e "${RED}⚠️ Warning: Cannot access directory '$dir'${NC}"
ERRORS=$((ERRORS + 1))
continue
}
# Process JPEG files in current directory
for f in *.jpeg; do
[ -f "$f" ] || continue
# Extract base name without extension
base="${f%.[jJ][pP][eE][gG]}"
newfile="${base}.jpg"
# Check if target file already exists
if [ -e "$newfile" ]; then
FILES_SKIPPED=$((FILES_SKIPPED + 1))
else
if mv "$f" "$newfile" 2>/dev/null; then
FILES_RENAMED=$((FILES_RENAMED + 1))
else
echo -e "${RED}⚠️ Warning: Failed to rename '$f' in directory '$dir'${NC}"
ERRORS=$((ERRORS + 1))
fi
fi
done
cd ..
done
echo ""
echo -e "${YELLOW}📈 Normalization Statistics:${NC}"
echo " Directories processed: $TOTAL_DIRS"
echo " Total JPEG files found: $TOTAL_JPEG_FILES"
echo -e " Files renamed: ${GREEN}$FILES_RENAMED${NC}"
echo -e " Files skipped (target exists): ${YELLOW}$FILES_SKIPPED${NC}"
echo -e " Errors encountered: ${RED}$ERRORS${NC}"
echo ""
if [ $ERRORS -eq 0 ]; then
echo -e "${GREEN}🎉 Normalization completed successfully!${NC}"
if [ $FILES_RENAMED -gt 0 ]; then
echo -e "${GREEN}✅ Successfully renamed $FILES_RENAMED JPEG files to JPG format${NC}"
fi
if [ $FILES_SKIPPED -gt 0 ]; then
echo -e "${YELLOW} Skipped $FILES_SKIPPED files (target .jpg files already existed)${NC}"
fi
else
echo -e "${YELLOW}⚠️ Normalization completed with $ERRORS errors${NC}"
echo -e "${YELLOW}💡 Please check the warnings above and resolve any issues${NC}"
fi
echo "========================================"