Last active
July 2, 2019 09:55
-
-
Save mir4a/bed6ef8ed6ae8432d7adf4055a7a219d to your computer and use it in GitHub Desktop.
Resize all image files in particular directory and put them in separate directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
while [ "$#" -gt 0 ]; do | |
case "$1" in | |
-w) width="$2"; shift 2;; | |
-h) height="$2"; shift 2;; | |
-p) filepath="$2"; shift 2;; | |
-m) mask="$2"; shift 2;; | |
--width=*) width="${1#*=}"; shift 1;; | |
--height=*) height="${1#*=}"; shift 1;; | |
--filepath=*) filepath="${1#*=}"; shift 1;; | |
--mask=*) mask="${1#*=}"; shift 1;; | |
--width|--height|--filepath|--mask) echo "$1 requires an argument" >&2; exit 1;; | |
-*) echo "unknown option: $1" >&2; exit 1;; | |
*) handle_argument "$1"; shift 1;; | |
esac | |
done | |
if [ "$filepath" = "" ]; then | |
echo "no --filepath option" | |
exit 2; | |
fi | |
if [ "$width" = "" ]; then | |
echo "no --width option" | |
exit 2; | |
fi | |
if [ "$height" = "" ]; then | |
echo "no --height option" | |
exit 2; | |
fi | |
cd "$filepath" || exit | |
files=$(ls *$mask*) | |
x="x" | |
newdir="$width$x$height" | |
echo "$newdir" | |
mkdir "$newdir" | |
echo $(ls) | |
processing() { | |
ident=$(gm identify "$1") | |
size=$(echo "$ident" | sed 's/.*\([0-9]\{4\}\)x\([0-9]\{4\}\).*/\1-\2/') | |
side_diff=$(($size)) | |
if [ $side_diff \> 0 ]; then | |
echo "processing $1 (landscape)" | |
gm convert -gravity center -quality 100 -resize "$width$x$height^" -crop "$width$x$height+0+0" "$1" "$newdir/$newdir---$1" | |
else | |
echo "processing $1 (portrait)" | |
gm convert -gravity center -quality 100 -resize "$height$x$width^" -crop "$height$x$width+0+0" "$1" "$newdir/$newdir---$1" | |
fi | |
} | |
# TODO: make progress bar | |
cores=4 | |
trap "exit" INT | |
for f in $files;do | |
processing $f & | |
background=( $(jobs -p) ) | |
echo ${#background[@]} | |
if (( ${#background[@]} \>= cores )); then | |
wait | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment