Skip to content

Instantly share code, notes, and snippets.

@zeromancer
Last active December 26, 2017 18:37
Show Gist options
  • Save zeromancer/99815cd9e49ae4c14c4b92f7f04bc413 to your computer and use it in GitHub Desktop.
Save zeromancer/99815cd9e49ae4c14c4b92f7f04bc413 to your computer and use it in GitHub Desktop.
Automatically processes all images in a folder to svg by reproducing the image with triangles
#!/bin/bash
set -x ## Print all executed commands to the terminal
set -e ## Exit immediately if a command exits with a non-zero status
# Example usage
# download/install https://github.com/fogleman/primitive
# cd ~/Develop/Go
# mkdir -p n100 n100/n100_src n100/n100_dst
# ./watch_convert_dir.sh n100 n100/n100_src n100/n100_dst 100 -r 64 bg #000000
# ./watch_convert_dir.sh n200 n200/n200_src n200/n200_dst 200 -m 0
#
# Parse arguments
#
function show_usage() {
echo -e "Usage: $0 src_dir fin_dir dst_dir n (optional: additional options for generation)"
}
if [[ $# -lt 4 ]]; then
show_usage
exit 0
fi
DIR="$1" # source dir: files to be converted
FNT_DIR="$2" # finished dir: source files to be moved elsewhere then generation finishes
DST_DIR="$3" # destination dir: for newly generated files
N="$4"
function convert_all_dir() {
for SRC_FILE in $(ls $DIR | egrep "\.png|\.jpg|\.PNG|\.JPG$"); do
DST_FILE=$(echo $DIR/$SRC_FILE | sed -e "s~png~svg~Ig" -e "s~jpg~svg~Ig" -e "s~$DIR~$DST_DIR~ig")
SRC_FILE_FINISHED=$(echo $DIR/$SRC_FILE | sed "s~$DIR~$FNT_DIR~g")
$GOPATH/bin/primitive -n $N -i $DIR/$SRC_FILE -o $DST_FILE "$@"
mv $DIR/$SRC_FILE $SRC_FILE_FINISHED
done
}
while true; do
if [ "$(ls $DIR | egrep "\.png|\.jpg|\.PNG|\.JPG$" | wc -l)" -gt 0 ]; then
#echo "$DIR is not Empty"
convert_all_dir "${@:5}"
else
#echo "$DIR is Empty"
sleep 2
fi
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment