Skip to content

Instantly share code, notes, and snippets.

@jameslafa
Last active December 8, 2020 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameslafa/840833d8ba31567e4dc25f183141a35b to your computer and use it in GitHub Desktop.
Save jameslafa/840833d8ba31567e4dc25f183141a35b to your computer and use it in GitHub Desktop.
Script to square pictures, add a white border and resize them to 1200px for instagram
#!/bin/bash
for dir in */ ; do
# Check if there are images in the folder before moving forward
if [[ $(find $dir -maxdepth 1 -type f -name "*.jpg"| wc -l) -ge 1 ]]; then
# If there is already an instagram folder in this folder, I skip.
if [[ ! -d "$dir"instagram ]]; then
mkdir "$dir"instagram
fi
for file in "$dir"*.jpg ; do
filename="${file##*/}"
if [[ -f "$dir"/instagram/"$filename" ]]; then
continue
fi
# Get picture dimentions (w & h)
#IFS=" x+" read w h x y < <(convert -fuzz 10% "$file" -format "%@" info:)
IFS=" x+" read w h x y < <(convert "$file" -format "%@" info:)
# Get longest side
longest=$w
[ $h -gt $longest ] && longest=$h
# Add 10% border
longest=$(echo "scale=0;$longest*1.05/1" | bc)
pic="$dir"instagram/$(basename "$file")
echo "$pic" # to see the progress
# convert -fuzz 10% "$file" -trim -background white -gravity center -extent ${longest}x${longest} "$pic"
convert "$file" -trim -background white -gravity center -extent ${longest}x${longest} -resize 1200x1200 "$pic"
done
fi
done
@jameslafa
Copy link
Author

jameslafa commented Feb 20, 2020

Example of the result


R1-00688-0026

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