Skip to content

Instantly share code, notes, and snippets.

@fetimo
Last active January 14, 2025 12:58
Show Gist options
  • Save fetimo/1f933077dd18e352e0efd9505e0bb048 to your computer and use it in GitHub Desktop.
Save fetimo/1f933077dd18e352e0efd9505e0bb048 to your computer and use it in GitHub Desktop.
A simple script to generate side-by-side before and after images using Imagemagick 7.1.1-43.
#!/bin/bash
# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <product_name> <hair_type>"
exit 1
fi
# Assign arguments to variables
product_name="$1"
hair_type="$2"
# Define the input images based on the arguments
image1="${hair_type}.jpg"
image2="${product_name}/${product_name}_${hair_type}.jpg"
# Get the dimensions of both images
dimensions1=$(identify -format "%wx%h" "$image1")
dimensions2=$(identify -format "%wx%h" "$image2")
# Extract width and height for both images
width1=$(echo $dimensions1 | cut -d'x' -f1)
height1=$(echo $dimensions1 | cut -d'x' -f2)
width2=$(echo $dimensions2 | cut -d'x' -f1)
height2=$(echo $dimensions2 | cut -d'x' -f2)
# Calculate the smallest common height
min_height=$((height1 < height2 ? height1 : height2))
# Calculate half widths
half_width1=$((width1 / 2))
half_width2=$((width2 / 2))
# Crop the left half of the first image to the smallest common height
magick "$image1" -crop "${half_width1}x${min_height}+0+0" left_half.jpg
# Crop the right half of the second image to the smallest common height
magick "$image2" -crop "${half_width2}x${min_height}+$half_width2+0" right_half.jpg
# Calculate the smallest common width after cropping
min_width=$((half_width1 < half_width2 ? half_width1 : half_width2))
# Further crop both halves to the smallest common width
magick left_half.jpg -crop "${min_width}x${min_height}+0+0" left_half_cropped.jpg
magick right_half.jpg -crop "${min_width}x${min_height}+0+0" right_half_cropped.jpg
# Combine the two halves into one image
output_file="${product_name}/${product_name}_${hair_type}_split.jpg"
magick left_half_cropped.jpg right_half_cropped.jpg +append "$output_file"
# Clean up temporary files
rm left_half.jpg right_half.jpg left_half_cropped.jpg right_half_cropped.jpg
echo "Output image created: $output_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment