#!/usr/bin/env bash
set -e
# ===============================================================
# PNG to JPEG Converter for ComfyUI Output
# ===============================================================
#
# Purpose:
# This script converts PNG images generated by ComfyUI to JPEG format.
# It's designed to process images in the 'comfyui-output/dev-dancing' 
# directory and save the converted files in a './covers' directory.
#
# Features:
# - Converts PNG to JPEG using ImageMagick (magick convert)
# - Applies '-strip' to remove metadata and '-quality 90' for compression
# - Skips already converted images to allow for incremental processing
# - Creates the output directory if it doesn't exist
#
# Usage:
# ./convert_to_jpg.sh
#
# Note: Ensure ImageMagick is installed on your system before running.
#
# Author: [Your Name]
# Date: [Current Date]
# Version: 1.0
#
# ===============================================================

if [ $(hostname) = "litten" ]; then
    rsync -tap roar:/opt/comfyui/output/dev-dancing comfyui-output/
fi

# Create the output directory if it doesn't exist
mkdir -p ./covers

# Loop through all PNG files in the comfyui-output/dev-dancing directory
for png_file in comfyui-output/dev-dancing/*.png; do
    # Get the base filename without extension
    base_name=$(basename "$png_file" .png)
    
    # Define the output JPEG filename
    jpg_file="./covers/${base_name}.jpg"
    
    # Check if the JPEG file already exists
    if [ ! -f "$jpg_file" ]; then
        # Convert PNG to JPEG using magick with specified options
        magick "$png_file" -strip -quality 90 "$jpg_file"
        echo "Converted: $png_file -> $jpg_file"
    else
        echo "Skipped: $jpg_file already exists"
    fi
done

echo "Conversion complete!"