Skip to content

Instantly share code, notes, and snippets.

@fofr
Created March 22, 2023 22:16
Show Gist options
  • Save fofr/e8ffad0f2a327eb4ebb2c90e75fb8525 to your computer and use it in GitHub Desktop.
Save fofr/e8ffad0f2a327eb4ebb2c90e75fb8525 to your computer and use it in GitHub Desktop.
A bash script to split a Midjourney v5 grid of images using ImageMagick
#!/bin/bash
# Check if 'convert' is available
if ! command -v convert &> /dev/null; then
echo "'convert' command not found. Please install ImageMagick and try again."
exit 1
fi
input_file="$1"
if [[ -z "$input_file" ]]; then
echo "Please provide an input file."
exit 1
fi
if [[ ! -f "$input_file" ]]; then
echo "Input file not found."
exit 1
fi
if [[ ${input_file##*.} != "png" ]]; then
echo "Please provide a .png file."
exit 1
fi
dir_path=$(dirname "$input_file")
file_name=$(basename "$input_file" .png)
output_prefix="${dir_path}/${file_name}"
width=$(identify -format "%w" "$input_file")
height=$(identify -format "%h" "$input_file")
half_width=$((width/2))
half_height=$((height/2))
convert "$input_file" -crop "${half_width}x${half_height}+0+0" "${output_prefix}-1.png"
convert "$input_file" -crop "${half_width}x${half_height}+${half_width}+0" "${output_prefix}-2.png"
convert "$input_file" -crop "${half_width}x${half_height}+0+${half_height}" "${output_prefix}-3.png"
convert "$input_file" -crop "${half_width}x${half_height}+${half_width}+${half_height}" "${output_prefix}-4.png"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment