バッチ画像スケーリング

大量の写真を拡大縮小する必要に直面しました。 もちろん、もっとエレガントなソリューションがあると思います、

しかし、Googleが手元になかったため、bashでimagemagickの前面をすばやくスケッチしました。 誰かが役に立つと思います。



#!/bin/bash



show_help() {

cat << EOF

:

$0 [ ]

:

--help

--resource-dir=DIR

--results-dir=DIR , , small

--apportion . , .

--resolution=RES , . , 1024x768 45%

EOF

exit 0

}



#

source_dir=$(pwd);

results_dir= "$source_dir/small" ;

apportion= "0" ;

resolution= "0" ;



# " "

for param in $@ ;

do

case $param in --help)

show_help;;

esac

done



#

for param in $@ ;

do

case $param in

--resource-dir=*) source_dir=$( echo $param | cut -d '=' -f 2 ) ;;

--results-dir=*) results_dir=$( echo $param | cut -d '=' -f 2 ) ;;

--apportion) apportion= "1" ;;

--resolution=*) resolution=$( echo $param | cut -d '=' -f 2 ) ;;

esac

done



#

#

if test -z "$1"

then

echo " ." ;

show_help;

fi



# resource-dir

if ! [ -d "$source_dir" ]

then

echo " $source_dir " ;

exit 0;

fi



#

if test "$resolution" = "0"

then

echo " ." ;

exit 0;

fi



if [ `expr index "$resolution" "x" ` = "0" ] && [ `expr index "$resolution" "%" ` = "0" ]

then

echo " " ;

exit 0;

fi



if ! [ -x `which identify` ]

then

echo ", identify " ;

exit 0;

fi



if ! [ -x `which convert` ]

then

echo ", convert " ;

exit 0;

fi



# , ...

if ! [ -d "$results_dir" ]

then

`mkdir "$results_dir" `;

fi



# ,

for pic in `ls "$source_dir" | grep -i "jpg" `

do

file_res=$( identify "$pic" | awk '{print $3}' );

width=$( echo "$file_res" | cut -d 'x' -f 1 );

height=$( echo "$file_res" | cut -d 'x' -f 2 );

if [ `expr index "$resolution" "%" ` != "0" ]

then

percent=$( echo "$resolution" | cut -d '%' -f 1 );

n_width=$( echo "$percent*$width/100" | bc | cut -d '.' -f 1 );

n_height=$( echo "$percent*$height/100" | bc | cut -d '.' -f 1 );

fi

if [ `expr index "$resolution" "x" ` != "0" ]

then

n_width=$( echo "$resolution" | cut -d 'x' -f 1 );

n_height=$( echo "$resolution" | cut -d 'x' -f 2 );



#

if [ "$apportion" = "1" ]

then

ratio=$( echo "scale=9; $width/$height" | bc );

if [ "$width" -ge "$height" ]

then

n_height=$( echo "$n_width/$ratio" | bc | cut -d '.' -f 1 );

else

n_width=$( echo "$n_height*$ratio" | bc | cut -d '.' -f 1 );

fi

fi

fi

convert -size "$file_res" "$source_dir/$pic" -resize "$n_width" 'x'"$n_height" +profile "*" "$results_dir"/"$pic"

echo " $pic $n_width $n_height" ;

done




* This source code was highlighted with Source Code Highlighter .






PS:スクリプトを何らかの方法でねじ込む必要がある場合は、透かしなどの追加を締めてください-コメントを書いてください...

PSPS:批判とより美しいオプションはいつでも歓迎です...



All Articles