Last active
June 28, 2018 01:49
-
-
Save smagch/93db58b9df272994d694216187d17ecb to your computer and use it in GitHub Desktop.
imagemagick bash script
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/bash | |
SIZES=(640 1280 1920 2560 3200) | |
if [ $# -eq 0 ]; then | |
echo "画像ファイルを指定してください!" | |
echo "Usage1: "$0" image.jpg" | |
echo "Usage2: "$0" *.jpg" | |
exit 1 | |
fi | |
main () { | |
for file in "$@" | |
do | |
echo "convert start: $file" | |
resize "$file" | |
echo "convert done: $file" | |
done | |
echo "all done!" | |
} | |
resize () { | |
target="$1" | |
extension="${target##*.}" | |
filename="${target%.*}" | |
if [ ! -f $target ]; then | |
echo "File $target not found" | |
exit 1 | |
fi | |
width=$(identify -format "%[fx:w]" $target) | |
cp "${target}" "${filename}_${width}.${extension}" | |
echo "clone file ${target} to ${filename}_${width}.${extension}" | |
for w in ${SIZES[@]}; do | |
if [ "$w" -gt "$width" ]; then | |
echo "end" | |
break | |
fi | |
convert "${target}" -resize "${w}x" -set filename:mysize '%w' "${filename}_%[filename:mysize].${extension}" | |
echo "converted to ${w}px" | |
done | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment