testsAndMisc/scripts/utils/image_to_resolution.sh
Krzysztof kuhy Rudnicki 3a62a02c3d refactor: reduce code duplication from 1.97% to 0.76%
- Add common.sh library functions: require_imagemagick, install_missing_pacman_packages, handle_arg_help_or_unknown
- Create android.sh shared library for Android utilities
- Create hosts-guard-common.sh for pacman hooks shared functions
- Update multiple scripts to source common.sh and use shared helpers
- Add print_shutdown_schedule helper in setup_midnight_shutdown.sh
- Remove duplicate log(), usage(), install_packages patterns across scripts
- Format all shell scripts with shfmt (2-space indent)
2025-12-11 18:32:15 +01:00

85 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Convert an image to a specified resolution
# Default resolution: 320x240
# Usage: image_to_resolution.sh <input_image> [resolution] [output_image]
set -euo pipefail
# Source common library for shared functions
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
# shellcheck source=../lib/common.sh
source "$SCRIPT_DIR/../lib/common.sh"
# Default resolution
DEFAULT_RESOLUTION="320x240"
# Function to display usage
usage() {
cat <<EOF
Usage: $0 <input_image> [resolution] [output_image]
Arguments:
input_image Path to the input image file (required)
resolution Target resolution in WIDTHxHEIGHT format (default: ${DEFAULT_RESOLUTION})
output_image Path to the output image file (default: <input>_<resolution>.<ext>)
Examples:
$0 photo.jpg
$0 photo.jpg 640x480
$0 photo.jpg 1920x1080 output.jpg
$0 image.png 320x240 resized.png
Note: Requires ImageMagick (convert command)
EOF
exit 1
}
# Check if ImageMagick is installed
require_imagemagick "convert" || exit 1
# Parse arguments
if [[ $# -lt 1 ]]; then
echo "Error: Missing required argument <input_image>"
usage
fi
INPUT_IMAGE="$1"
RESOLUTION="${2:-${DEFAULT_RESOLUTION}}"
OUTPUT_IMAGE="${3:-}"
# Validate input image exists
if [[ ! -f ${INPUT_IMAGE} ]]; then
echo "Error: Input image '${INPUT_IMAGE}' does not exist."
exit 1
fi
# Validate resolution format (WIDTHxHEIGHT)
if ! validate_resolution "$RESOLUTION"; then
echo "Error: Invalid resolution format '${RESOLUTION}'"
echo "Expected format: WIDTHxHEIGHT (e.g., 320x240, 1920x1080)"
exit 1
fi
# Generate output filename if not provided
if [[ -z ${OUTPUT_IMAGE} ]]; then
OUTPUT_IMAGE=$(generate_output_filename "${INPUT_IMAGE}" "_${RESOLUTION}")
fi
# Perform the conversion
echo "Converting '${INPUT_IMAGE}' to ${RESOLUTION}..."
echo "Output will be saved to: ${OUTPUT_IMAGE}"
if convert "${INPUT_IMAGE}" -resize "${RESOLUTION}!" "${OUTPUT_IMAGE}"; then
echo "✓ Successfully converted image to ${RESOLUTION}"
echo "Output: ${OUTPUT_IMAGE}"
# Show file sizes
INPUT_SIZE=$(du -h "${INPUT_IMAGE}" | cut -f1)
OUTPUT_SIZE=$(du -h "${OUTPUT_IMAGE}" | cut -f1)
echo "Input size: ${INPUT_SIZE}"
echo "Output size: ${OUTPUT_SIZE}"
else
echo "✗ Error: Conversion failed"
exit 1
fi