testsAndMisc/linux_configuration/scripts/single_use/utils/image_to_resolution.sh
Krzysztof kuhy Rudnicki 42a66a1419 refactor(linux_configuration/scripts): split all scripts into single_use/ and periodic_background/
- Move all linux_configuration scripts into two semantic categories:
  - single_use/: scripts run once manually (fresh install, fixes, setup)
  - periodic_background/: scripts run by systemd timers or daemons
- Preserve existing subdirectory structure within each category
- Fix lib/common.sh source paths for new directory depths
- Fix CONFIG_DIR depth in setup_periodic_system.sh and check_and_enable_services.sh
- Update all references in tests, fresh-install/main.sh, nix modules, and docs
- Fix check_polling_antipatterns.sh false positives (||, regex |, case patterns, jq strings)
- Fix pre-existing mypy exclusion path and type annotations for moved tools/ directory
- Rewrite check_polling_antipatterns.sh using awk (no bash regex loops); add require_serial: true
2026-05-15 00:32:35 +02: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