Created
December 10, 2013 21:50
-
-
Save mattrasband/7900881 to your computer and use it in GitHub Desktop.
Batch watermark images using `composite` from Image-Magick
This file contains hidden or 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
#!/usr/bin/env bash | |
#============================================ | |
# Apply a watermark to a batch of images | |
# in the source directory after verifying | |
# that the images are not corrupted. | |
# This applies the watermark based off | |
# the source image's resolution. | |
# (recommended WM base size of 500px wide) | |
#============================================ | |
# Author: Matthew Rasband (nerdwaller) | |
#============================================ | |
if [[ ${1} == "-h" ]]; then | |
echo "Usage: ${0} -i [src] -o [dest] -w [watermark] -e [ext] -l [WatermarkLoc]" | |
echo | |
echo "-i [src]\t\tDefault: `pwd`" | |
echo "-o [dest]\t\tDefault: `pwd`/batch_watermarked" | |
echo "-w [watermark]\t\tDefault: edgeWatermark.png (retrieved automatically)" | |
echo "-l [WatermarkLoc]\t\tDefault: SouthEast [Options: Center, North/South{,West,Center,East}, East, West" | |
echo "-e [ext]\t\tDefault: JPG/jpg" | |
exit 1 | |
fi | |
while [ $# -gt 1 ]; do | |
case $1 in | |
-i) SRC=${2}; shift 2;; | |
-o) DEST=${2}; shift 2;; | |
-w) WM=${2}; shift 2;; | |
-l) WMLoc=${2}; shift 2;; | |
-e) EXT=${2}; shift 2;; | |
*) shift 1;; | |
esac | |
done | |
function setUnsets { | |
if [ -z ${SRC} ]; then | |
SRC=`pwd` | |
fi | |
if [ -z ${DEST} ]; then | |
DEST=${SRC}/batch_watermarked | |
fi | |
if [ -z ${WM} ]; then | |
WM=edgeWatermark.png | |
fi | |
if [ -z ${WMLoc} ]; then | |
WMLoc="SouthEast" | |
fi | |
if [ -z ${EXT} ]; then | |
EXT="jpg" | |
fi | |
LOG=${SRC}/batch_brokenImage.log | |
TMP=${SRC}/temp | |
BROKEN=${TMP}/broken | |
} | |
function checkMakeResources { | |
echo; echo "Checking for the necessary resources." | |
for dir in ${DEST} ${TMP} ${BROKEN}; do | |
if [[ ! -d ${dir} ]]; then | |
echo "Making directory: ${dir}" | |
mkdir ${dir} | |
fi | |
done | |
if [ ! -e ${WM} ]; then | |
echo "Getting the default watermark from EDGE-RL" | |
wget https://www.edge-rl.org/media/img/linked/edgeWatermark.png | |
mv ${WM} ${TMP} | |
fi | |
} | |
function copyToTemp { | |
echo "Copying originals to ${TMP}" | |
for img in `find ${SRC} -maxdepth 1 -type f -iname "*.${EXT}"`; do | |
cp ${img} ${TMP} | |
done | |
} | |
function checkForInvalidImages { | |
for img in `find ${TMP} -type f -iname "*.${EXT}"`; do | |
if ! identify "${img}" &> /dev/null; then | |
echo "Corrupted ${EXT} found: ${img} -- moving to ${BROKEN}" | |
echo "You can see the broken image log at ${LOG}" | |
echo `date`":" ${img} >> ${BROKEN} | |
mv ${img} ${BROKEN} | |
fi | |
done | |
} | |
function fixOrientation { | |
cd ${TMP} | |
echo "Auto-Orienting the files in ${TMP}" | |
for img in `find . -maxdepth 1 -type f -iname "*.${EXT}"`; do | |
img=`echo ${img} | sed 's:./::g'` | |
convert -auto-orient ${img} ao.${img} | |
rm -f ${img} | |
done | |
cd ${SRC} | |
} | |
function applyWatermark { | |
cd ${TMP} | |
for valid in `find . -maxdepth 1 -type f -iname "ao.*.${EXT}"`; do | |
local wmscale=100% | |
echo "Checking image size and watermarking..." | |
local resolution=`identify ${valid} | awk '{print $3}'` | |
local width=`echo ${resolution} | cut -f1 -d "x"` | |
if [[ ${width} -gt "3999" ]]; then | |
wmscale=100% | |
elif [[ ${width} -lt "4000" && ${width} -gt "3499" ]]; then | |
wmscale=85% | |
elif [[ ${width} -lt "3500" && ${width} -gt "2999" ]]; then | |
wmscale=75% | |
elif [[ ${width} -lt "3000" && ${width} -gt "2499" ]]; then | |
wmscale=65% | |
elif [[ ${width} -lt "2500" && ${width} -gt "1999" ]]; then | |
wmscale=55% | |
elif [[ ${width} -lt "2000" && ${width} -gt "1499" ]]; then | |
wmscale=45% | |
elif [[ ${width} -lt "1500" && ${width} -gt "999" ]]; then | |
wmscale=35% | |
else | |
wmscale=25% | |
fi | |
echo "${valid}: ${resolution} using ${wmscale} for the watermark." | |
echo "Watermarking ${valid} and saving to ${DEST}" | |
composite -dissolve 100% -gravity ${WMLoc} -quality 100 \( ${WM} -resize ${wmscale} \) ${valid} ${DEST}/`echo ${valid} | awk -F/ '{print $NF}' | sed -e 's/ao./wm./g' -e "s/\.${EXT^^}/\.${EXT,,}/g"` | |
done | |
cd ${SRC} | |
} | |
function cleanUp { | |
echo "Cleaning up..."; echo | |
rm -rf ${TMP} | |
rm -f ${WM} | |
echo "Finished." | |
} | |
function main { | |
setUnsets | |
checkMakeResources | |
copyToTemp | |
checkForInvalidImages | |
fixOrientation | |
applyWatermark | |
cleanUp | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment