How do I obtain good pointsize for text depending on the image I print the text on?
Particularly, I'm making script to stamp watermakrs automatically on a wide variety of images(canvas sizes). I've made bash script for testing:
Code: Select all
#!/bin/bash -
#{{{ Parse CLI args
function my_split_str()
{
if (( $# <=1 ))
then
IFS='='
else
IFS=$2
fi
set -- $1
echo $*
}
# For string like part1=part2
# returns part2
function my_arg_val()
{
echo $1 | sed 's/[-a-zA-Z0-9]*=//'
}
# Get args
for i in $*
do
case $i in
$1*)
SRC_IMG=`my_arg_val $i`
;;
--src-img=*)
SRC_IMG=`my_arg_val $i`
;;
--wm-width=*)
WM_WIDTH=`my_arg_val $i`
;;
--wm-height=*)
WM_HEIGHT=`my_arg_val $i`
;;
--font-size=*)
WM_FONT_SIZE=`my_arg_val $i`
;;
*)
# unknown option
#a=(`my_split_str $i '='`)
#echo "Unknown option ${a[0]}"
#exit 2
;;
esac
done
#}}}
OUT_IMG="out.jpg"
if [ -z $SRC_IMG ]; then
echo "--src-img";
exit 2
fi
#{{{ Arg defaults
: ${WM_TEXT:="megagroup.ru"}
: ${WM_WIDTH:=300}
: ${WM_HEIGHT:=50}
: ${WM_FONT:="DejaVu-Sans-Condensed"}
: ${WM_FONT_SIZE:=20}
: ${WM_FG_IMAGE:="stamp_fg.png"}
: ${WM_BG_IMAGE="stamp_bg.png"}
: ${WM_STAMP_MASK="stamp_mask.png"}
: ${WM_STAMP="stamp.png"}
: ${WM_GRAVITY:="south-west"}
#}}}
convert -size $WM_WIDTH"x"$WM_HEIGHT xc:transparent \
-font "$WM_FONT" -pointsize $WM_FONT_SIZE\
-draw "gravity center \
fill grey15 text 0,0 '$WM_TEXT' \
fill white text 8,8 '$WM_TEXT' " \
"$WM_STAMP"
mogrify -trim +repage "$WM_STAMP"
composite -dissolve 25% \
-gravity "$WM_GRAVITY" \
"$WM_STAMP" "$SRC_IMG" "$OUT_IMG"
original image and height picked roughly, ex. 300px. Then it trims and repages
watermark image and applies it on original image via "dissolve".
Thus, the watermark first canvas size doesn't matter. The only thing I have to
adjust here is pointsize. So how do I determine what pointsize to pick for
arbitrary original image size?
With the following command pointsize of 220 results font height of about 134px:
Code: Select all
./test.sh --src-img=a.jpg --wm-width=2200 --wm-height=300 --font-size=220
Regards.