Skip to content

Instantly share code, notes, and snippets.

@Aldo-f
Created July 29, 2024 07:27
Show Gist options
  • Save Aldo-f/be56c5d3e631302c19a105f05c4132e5 to your computer and use it in GitHub Desktop.
Save Aldo-f/be56c5d3e631302c19a105f05c4132e5 to your computer and use it in GitHub Desktop.
batch_convert.sh - convert JPG to a reduced size
#!/bin/bash
# Default prefixes for parameters
QUALITY_PREFIX="-q"
SUFFIX_PREFIX="-s"
MAX_DEPTH_PREFIX="-d"
MAX_FILES_PREFIX="-m"
# Check if the quality parameter is provided
if [ -z "$2" ] || [ "$1" != "$QUALITY_PREFIX" ]; then
echo "Usage: $0 $QUALITY_PREFIX <quality> [$SUFFIX_PREFIX <suffix>] [$MAX_DEPTH_PREFIX <max-depth>] [$MAX_FILES_PREFIX <max-files>]"
exit 1
fi
quality=$2
suffix=""
max_depth=1
max_files=0
# Check if the suffix parameter is provided
if [ ! -z "$4" ] && [ "$3" == "$SUFFIX_PREFIX" ]; then
suffix="_$4"
if [ ! -z "$6" ] && [ "$5" == "$MAX_DEPTH_PREFIX" ]; then
max_depth=$6
if [ ! -z "$8" ] && [ "$7" == "$MAX_FILES_PREFIX" ]; then
max_files=$8
fi
elif [ ! -z "$6" ] && [ "$5" == "$MAX_FILES_PREFIX" ]; then
max_files=$6
fi
elif [ ! -z "$4" ] && [ "$3" == "$MAX_DEPTH_PREFIX" ]; then
max_depth=$4
if [ ! -z "$6" ] && [ "$5" == "$MAX_FILES_PREFIX" ]; then
max_files=$6
fi
elif [ ! -z "$4" ] && [ "$3" == "$MAX_FILES_PREFIX" ]; then
max_files=$4
fi
# Get the current working directory where the command is executed
current_dir=$(pwd)
echo "Current directory: $current_dir"
# Calculate the total size of the original files
original_size=0
files_converted=0
# Find files and store their size
files=$(find "$current_dir" -maxdepth "$max_depth" -type f -name "*.jpg")
if [ -z "$files" ]; then
echo "No JPEG files found in the specified directory."
exit 1
fi
# Process each file and track its original size
declare -A original_sizes
for file in $files; do
size=$(stat -c%s "$file")
original_sizes["$file"]=$size
original_size=$((original_size + size))
done
# Loop through all JPEG files in the current directory and subdirectories up to the specified depth
for file in $files; do
if [ $max_files -gt 0 ] && [ $files_converted -ge $max_files ]; then
break
fi
filename=$(basename "$file")
filename_noext="${filename%.*}"
output_file="$(dirname "$file")/${filename_noext}${suffix}.jpg"
convert "$file" -quality "$quality" "$output_file"
files_converted=$((files_converted + 1))
done
# Calculate the total size of the new files
new_size=0
declare -A new_sizes
for file in $(find "$current_dir" -maxdepth "$max_depth" -type f -name "*${suffix}.jpg"); do
size=$(stat -c%s "$file")
new_sizes["$file"]=$size
new_size=$((new_size + size))
done
# Check if the original size is greater than zero to avoid division by zero
if [ $original_size -gt 0 ]; then
# Calculate the saved space
saved_size=$((original_size - new_size))
saved_percentage=$(awk "BEGIN {printf \"%.2f\", (($original_size - $new_size) / $original_size) * 100}")
else
saved_size=0
saved_percentage=0
fi
# Convert bytes to a more readable format
convert_size() {
local size=$1
if [ $size -lt 1024 ]; then
echo "${size}B"
elif [ $size -lt 1048576 ]; then
echo "$(awk "BEGIN {printf \"%.2f\", $size/1024}")KB"
elif [ $size -lt 1073741824 ]; then
echo "$(awk "BEGIN {printf \"%.2f\", $size/1048576}")MB"
else
echo "$(awk "BEGIN {printf \"%.2f\", $size/1073741824}")GB"
fi
}
# Provide a summary of the saved space
echo "Original size: $(convert_size $original_size)"
echo "New size: $(convert_size $new_size)"
echo "Saved size: $(convert_size $saved_size)"
echo "Saved percentage: $saved_percentage%"
echo "Files converted: $files_converted"
# Provide details of size reduction per file
echo "Size reduction per file:"
for file in "${!original_sizes[@]}"; do
new_file="${file%.*}${suffix}.jpg"
if [[ -f "$new_file" ]]; then
original_file_size=${original_sizes[$file]}
new_file_size=${new_sizes[$new_file]}
size_reduced=$((original_file_size - new_file_size))
# Only print files that have been reduced
if [ $size_reduced -gt 0 ]; then
size_reduction_percentage=$(awk "BEGIN {printf \"%.2f\", ($size_reduced / $original_file_size) * 100}")
echo "$(basename "$file") - Original size: $(convert_size $original_file_size), New size: $(convert_size $new_file_size), Reduced size: $(convert_size $size_reduced), Reduction percentage: $size_reduction_percentage%"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment