Skip to content

Instantly share code, notes, and snippets.

@swarminglogic
Last active December 1, 2015 21:53
Show Gist options
  • Save swarminglogic/e563571ec4d11ec0901b to your computer and use it in GitHub Desktop.
Save swarminglogic/e563571ec4d11ec0901b to your computer and use it in GitHub Desktop.
Subtlemark: Tool for adding subtle watermarks to images. Parameter customization: position (TL,TC,TR,BL,BC,BR), font, font-size, caption, textcolor, bgcolor. Also supports predefined styles.
#!/bin/bash
version=0.1.4
versionDate="2014-09-02"
function showHelp() {
echo "subtlemark - add subtle watermarks to images
Usage:
----------------------------------------
\$ subtlemark [options] FILE OUTPUT
Options:
-e, --extend If set, image is extended by label.
-s STYLE, --style STYLE Select a user defined style
-c COLOR, --color COLOR Override text color
-b COLOR, --bgcolor COLOR Override background color
-f FONT, --font FONT Override font name
-F SIZE, --font-size SIZE Override font point size
-t TEXT, --text TEXT Override image caption
-p PLACE, --place PLACE Override caption location
Where PLACE is any of:
NorthWest, North, NorthEast,
SouthWest, South, SouthEast
Other:
-h, --help Show help
-V, --version Outputs version information
-v, --verbose Enables verbose mode
Examples
----------------------------------------
Add watermark w/default settings
\$ subtlemark image.png out.png
Extend image to add watermark w/default settings
\$ subtlemark -e image.png out.png
Add watermark using style \"A\"
\$ subtlemark -s A image.png out.png
Add watermark with solid white bakground and red text
\$ subtlemark -c red -b white image.png out.png
Same as above, with RGB color code
\$ subtlemark -c '#F00' -b '#FFF' image.png out.png
Transparent background
\$ subtlemark -b none image.png out.png
Semi-Transparent text and background color (#RGBA)
\$ subtlemark -c '#FF05BB77' -b '#7775' image.png out.png
Custom caption
\$ subtlemark -t \"This is my caption\" image.png out.png
Specify font and font size to use
\$ subtlemark -f 'Verdana-Regular' image.png out.png
Place the watermark on top left corner
\$ subtlemark -p NorthWest image.png out.png
Mainted at: https://gist.github.com/swarminglogic/e563571ec4d11ec0901b
Author: Roald Fernandez (github@swarminglogic.com)
Version: $version ($versionDate)
License: CC-zero (public domain)
"
exit $1
}
leftovers=""
function parseParameters() {
while test $# -gt 0; do
case "$1" in
-h|--help)
showHelp 0 ;;
-V|--version)
shift ; echo "subtlemark $version" ; exit 1 ;;
-e|--extend)
shift ; param_is_extended=true ;;
-s|--style)
shift ; param_style=$1 ; shift ;;
-c|--color)
shift ; param_color=$1 ; shift ;;
-b|--bgcolor)
shift ; param_bgcolor=$1 ; shift ;;
-f|--font)
shift ; param_font=$1 ; shift ;;
-F|--font-size)
shift ; param_fontsize=$1 ; shift ;;
-t|--text)
shift ; param_caption=$1 ; shift ;;
-p|--place)
shift ; param_placement=$1 ; shift ;;
-v|--verbose)
shift ; is_verbose=true ;;
*)
leftovers="$leftovers "$1 ; shift ;;
esac
done
}
# Creates a dummy text image to determine line height of font/fontsize combo
# $1 fontname $2 pointsize
function computeFontLineHeight() {
if ! identify -list font | grep -q "Font: $1\$" ; then
echo "Error: Could not locate font: $1" >&2
if identify -list font | grep -qi "Font: .*$1" ; then
echo "Try one of these:" >&2
identify -list font | grep -i "Font: .*$1" >&2
fi
return 1;
fi
tmpfile=$(tempfile --suffix ".png")
convert -background none -fill black \
-font "$1" -pointsize $2 label:'abcXYZ' $tmpfile
echo $(identify -format %h $tmpfile)
rm $tmpfile
return 0;
}
# Parse parameters, or exit with help if none
if [ $# -gt 0 ] ; then parseParameters "$@" ; else
echo "Error: Missing parameters. See: $0 --help" >&2
exit 1
fi
# Set $leftovers as script parameters
# Check if either or both FILE OUTPUT is missing:
set -- $leftovers
if [ ! $# -eq 2 ] ; then
echo "Error: Missing input/output parameters" >&2
exit 1
fi
# Validate input file
file=$1
ofile=$2
# Configure based on specified styles:
if [ -n "$param_style" ] ; then
# Dummy style "A". Only include desired fields
if [ "$param_style" == "A" ] ; then
color="#AAA"
bgcolor="#F00"
font="Arial-Regular"
fontsize=10
is_extended=false
placement=SouthEast
caption="Roald Fernandez swarminglogic.com CC-BY"
else
echo "Error: Unknown style: $param_style" >&2
exit 1
fi
fi
# Priority of paramter configurations:
# [User override] >> [Style Override] >> [Defaults]
is_extended=${param_is_extended:-${is_extended:-false}}
color=${param_color:-${color:-"#8888"}}
bgcolor=${param_bgcolor:-${bgcolor:-"#3339"}}
font=${param_font:-${font:-"Silkscreen"}}
fontsize=${param_fontsize:-${fontsize:-8}}
font_line_height=$(computeFontLineHeight "$font" "$fontsize")
[ $? -eq 1 ] && exit 1;
if [ -n "$param_placement" ] ; then
if [ "$param_placement" != NorthWest ] && \
[ "$param_placement" != North ] && \
[ "$param_placement" != NorthEast ] && \
[ "$param_placement" != SouthWest ] && \
[ "$param_placement" != South ] && \
[ "$param_placement" != SouthEast ] ; then
echo "Error: Bad placement parameter: $param_placement" >&2
exit 1
fi
fi
placement=${param_placement:-${placement:-SouthEast}}
default_caption="Roald Fernandez swarminglogic.com $(date +'%Y-%m-%d') CC-BY"
caption=${param_caption:-${caption:-$default_caption}}
if [ "$is_verbose" == "true" ] ; then
echo "------------ VERBOSE ------------"
echo "Input file: ${file}"
echo "Output file: ${ofile}"
echo "Is Extended: ${is_extended}"
echo "Style: ${param_style:-N/A}"
echo "Text color: ${color}"
echo "BG color: ${bgcolor}"
echo "Text: ${caption}"
echo "Placement: ${placement}"
echo "Font: ${font}"
echo "Font size: ${fontsize} "
echo "Font line height (calculated): ${font_line_height:-N/A}"
echo "---------------------------------"
fi
# Check of input file exists.
if [ ! -e "$file" ] ; then
echo "Error: Cannot access input file: $file" >&2
exit 1
fi
if [ "$placement" == NorthWest ] || \
[ "$placement" == North ] || \
[ "$placement" == NorthEast ] ; then
is_top=true
fi
if [ "$is_extended" == "true" ] ; then
[ "$is_top" == true ] && swap="+swap"
convert $file -background "$bgcolor" \
-font "$font" \
-pointsize $fontsize \
-fill "$color" \
label:" $caption " \
$swap -gravity $placement -append $ofile
else
width=$(identify -format %w $file)
height=$(identify -format %h $file)
if [ "$is_top" == true ] ; then
rect="0,0,${width},${font_line_height}"
else
rect="0,$((height-$font_line_height)),${width},${height}"
fi
convert $file \
-fill "$bgcolor" -draw "rectangle ${rect}" \
-font "$font" \
-pointsize $fontsize \
-fill "$color" \
-gravity $placement \
-draw "text 0,1 ' $caption '" \
$ofile
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment