Skip to content

Instantly share code, notes, and snippets.

@safferli
Created September 18, 2023 14:25
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 safferli/44b4c83f8af407a090ba808c8fec4daf to your computer and use it in GitHub Desktop.
Save safferli/44b4c83f8af407a090ba808c8fec4daf to your computer and use it in GitHub Desktop.
rename szi images to make them easily imagemagick-able
#!/bin/zsh
# szi file[1] setup is:
#
# col_row.jpg
#
# for imagemagick we want:
#
# row_col.jpg
#
# and make sure to pad numbers for lexicographical sorting.
#
# [1] https://github.com/smartinmedia/SZI-Format
for file in *.jpg; do
# =~ is the regex matching operator in zsh
if [[ $file =~ ^([0-9]+)_([0-9]+)\.jpg$ ]]; then
col=${match[1]}
row=${match[2]}
# Pad col and row with leading zeros to ensure triple digits sorting
col=$(printf "%03d" $col)
row=$(printf "%03d" $row)
# swap col and row, and add r_, c_ to make sure files don't overwrite each other
new_file="r${row}_c${col}.jpg"
cp "$file" "$new_file"
echo "Renamed $file to $new_file"
fi
done
# call imagemagick with:
# magick montage -mode concatenate -tile 82x r*.jpg ../montage.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment