#!/bin/bash # Function to display help message function usage { echo "Usage: $0 -i image_path [-s width height] [-n columns rows]" echo " -i image_path : Path to the sprite sheet image" echo " -s width height : Size of a single sprite (in pixels)" echo " -n columns rows : Number of columns and rows in the sprite sheet" exit 1 } # Initialize variables img_path="" sprite_width="" sprite_height="" columns="" rows="" # Parse command line options while getopts "i:s:n:" opt; do case $opt in i) img_path=$OPTARG ;; s) sprite_width=${OPTARG} sprite_height=${!OPTIND} OPTIND=$(($OPTIND + 1)) ;; n) columns=${OPTARG} rows=${!OPTIND} OPTIND=$(($OPTIND + 1)) ;; *) usage ;; esac done # Ensure image path is provided if [[ -z "$img_path" ]]; then echo "Error: Image path is required." usage fi # Get image dimensions img_info=$(magick identify "$img_path") img_width=$(echo "$img_info" | cut -d ' ' -f 3 | cut -d 'x' -f 1) img_height=$(echo "$img_info" | cut -d ' ' -f 3 | cut -d 'x' -f 2) # Calculate sprite width/height from columns/rows, if provided if [[ -n "$columns" && -n "$rows" ]]; then sprite_width=$((img_width / columns)) sprite_height=$((img_height / rows)) elif [[ -z "$sprite_width" || -z "$sprite_height" ]]; then echo "Error: Either sprite size (-s) or number of columns and rows (-n) must be specified." usage fi # Output info echo "Splitting image $img_path into sprites of size ${sprite_width}x${sprite_height}..." # Use ImageMagick to split the sprite sheet magick convert "$img_path" -crop "${sprite_width}x${sprite_height}" +repage sprite_%d.png echo "Sprites successfully split into individual images (sprite_0.png, sprite_1.png, etc.)"