Skip to content

Instantly share code, notes, and snippets.

@jasalt
Created February 21, 2025 08:45
Show Gist options
  • Save jasalt/7edee658e2da4de331078455ad135e56 to your computer and use it in GitHub Desktop.
Save jasalt/7edee658e2da4de331078455ad135e56 to your computer and use it in GitHub Desktop.
#!/bin/bash
input_image="full.jpg"
output_prefix="part_"
max_width=3840
max_height=2160
aspect_ratio=$(bc -l <<< "$max_width / $max_height")
# Get image dimensions
dimensions=$(identify -ping -format "%w %h" "$input_image")
width=$(echo $dimensions | cut -d' ' -f1)
height=$(echo $dimensions | cut -d' ' -f2)
# Calculate number of parts
num_parts=$(((width + max_width - 1) / max_width))
# Calculate part width
part_width=$((width / num_parts))
part_height=$(bc <<< "scale=0; $part_width / $aspect_ratio / 1")
if [ $part_height -gt $max_height ]; then
part_height=$max_height
part_width=$(bc <<< "scale=0; $part_height * $aspect_ratio / 1")
fi
# Split the image
for ((i=0; i<num_parts; i++)); do
x_offset=$((i * part_width))
convert "$input_image" -crop ${part_width}x${part_height}+${x_offset}+0 "${output_prefix}${i}.jpg"
done
@jasalt
Copy link
Author

jasalt commented Feb 21, 2025

If errors with image size being too much for convert:

sudo vi /etc/ImageMagick-6/policy.xml

# Modify following, (defaults to 16KP on Debian 12):
  <policy domain="resource" name="width" value="16MP"/>
  <policy domain="resource" name="height" value="16MP"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment