|
#!/usr/bin/env bash |
|
|
|
# Script to crop an input image using ImageMagick's convert tool |
|
# Creates an output image named after the input with "-output" suffix |
|
# The script crops from Y coordinate 1034 to 1080, and X from 0 to 1920 |
|
|
|
# Here is the prompt I used with VSCode agent mode on April 12, 2025 |
|
# running against Claude 3.7 Sonnet |
|
# make a bash script in this file that follows the best practices from |
|
# https://bertvv.github.io/cheat-sheets/Bash.html #fetch . The bash script should |
|
# use the convert tool from the ImageMagick program to process the input image and |
|
# create an output image. Give a helpful error message if the convert tool is not |
|
# available, with instructions for common ways to install the program with homebrew |
|
# or apt. The script should take a single argument as the source image, and create |
|
# an output file that is named the same as the source image but with "-output" |
|
# added as a suffix to the basename of the input file. Keep the output file |
|
# extension the same as the input file extension. Validate that the input file is a |
|
# valid PNG file with 1920x1080 dimensions. The processing of the image should be |
|
# to -crop the image starting at Y coordinate offset pixel 1034 and continuing to |
|
# Y-coordinate pixel 1080. The X coordinate offset should be zero, and the |
|
# xcoordinate end point should be 1920. |
|
|
|
set -o errexit # abort on nonzero exit status |
|
set -o nounset # abort on unbound variable |
|
set -o pipefail # don't hide errors within pipes |
|
|
|
# Function for error messages |
|
error() { |
|
printf "\033[0;31m!!! %s\033[0m\n" "${*}" 1>&2 |
|
} |
|
|
|
# Function for success messages |
|
success() { |
|
printf "\033[0;32m=== %s\033[0m\n" "${*}" |
|
} |
|
|
|
# Function for info messages |
|
info() { |
|
printf "\033[0;34m--- %s\033[0m\n" "${*}" |
|
} |
|
|
|
# Function to check if ImageMagick is installed |
|
check_imagemagick() { |
|
if ! command -v magick > /dev/null 2>&1; then |
|
error "ImageMagick 'magick' tool not found!" |
|
printf "Please install ImageMagick using one of these methods:\n\n" |
|
printf " • macOS (Homebrew): brew install imagemagick\n" |
|
printf " • Ubuntu/Debian: sudo apt update && sudo apt install -y imagemagick\n" |
|
printf " • CentOS/RHEL: sudo yum install -y imagemagick\n" |
|
printf " • Windows: Download from https://imagemagick.org/script/download.php\n" |
|
exit 1 |
|
fi |
|
} |
|
|
|
# Function to display usage information |
|
usage() { |
|
cat << HELPMSG |
|
Usage: $(basename "${0}") [IMAGE_FILE] |
|
|
|
Process a PNG image (1920x1080) by cropping to keep just the bottom portion. |
|
Creates an output file with "-output" added to the original filename. |
|
|
|
Example: $(basename "${0}") my-image.png |
|
This will create my-image-output.png |
|
HELPMSG |
|
} |
|
|
|
# Function to validate the input image |
|
validate_image() { |
|
local image_file="${1}" |
|
|
|
# Check if file exists |
|
if [[ ! -f "${image_file}" ]]; then |
|
error "Input file does not exist: ${image_file}" |
|
exit 1 |
|
fi |
|
|
|
# Get image dimensions and type using identify |
|
local dimensions |
|
dimensions=$(identify -format "%w %h %m" "${image_file}" 2>/dev/null) || { |
|
error "Failed to identify image. Is it a valid image file?" |
|
exit 1 |
|
} |
|
|
|
# Parse dimensions and type |
|
local width height type |
|
read -r width height type <<< "${dimensions}" |
|
|
|
# Check if image is PNG |
|
if [[ "${type}" != "PNG" ]]; then |
|
error "Input file must be a PNG image. Found: ${type}" |
|
exit 1 |
|
fi |
|
|
|
# Check dimensions |
|
if [[ "${width}" -ne 1920 || "${height}" -ne 1080 ]]; then |
|
error "Input image must be 1920x1080 pixels. Found: ${width}x${height}" |
|
exit 1 |
|
fi |
|
} |
|
|
|
# Main function to process the image |
|
process_image() { |
|
local input_file="${1}" |
|
local base_name extension output_file |
|
|
|
# Extract filename and extension |
|
extension="${input_file##*.}" |
|
base_name="$(basename "${input_file}" ".${extension}")" |
|
output_file="${base_name}-output.${extension}" |
|
|
|
info "Processing image: ${input_file}" |
|
info "Output will be saved as: ${output_file}" |
|
|
|
# Calculate crop height: Y from 1034 to 1080 means a height of (1080-1034) pixels |
|
local crop_height=$((1080 - 1034)) |
|
|
|
# Process image using magick convert |
|
# Starting at x=0, y=1034 and keeping width=1920, height=224 pixels |
|
magick "${input_file}" -crop "1920x${crop_height}+0+1034" "${output_file}" || { |
|
error "Failed to process the image." |
|
exit 1 |
|
} |
|
|
|
success "Image processing completed successfully!" |
|
success "Output saved as: ${output_file}" |
|
} |
|
|
|
# Main execution |
|
main() { |
|
# Check for help argument |
|
if [[ $# -eq 0 || "${1-}" == "--help" || "${1-}" == "-h" ]]; then |
|
usage |
|
exit 0 |
|
fi |
|
|
|
# Check if ImageMagick is installed |
|
check_imagemagick |
|
|
|
# Validate and process the image |
|
validate_image "${1}" |
|
process_image "${1}" |
|
} |
|
|
|
# Execute main function |
|
main "${@}" |
also changed from the
convert
command to themagick
command because ImageMagick was printing warnings about deprecation and saying to usemagick
instead.