Created
February 22, 2020 12:38
-
-
Save anavarre/6bf02b5cf13d64f97b8562dd2a65c8fe to your computer and use it in GitHub Desktop.
ImageMagick: resize and watermark
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 | |
export ROOT_DIR="/path/to/pics" | |
function userInput() { | |
echo -e "Warning: I can only resize and watermark images in ${ROOT_DIR}.\n" | |
read -p "What is the directory name in which to resize and watermark images? " DIR | |
export INPUT_DIR="${ROOT_DIR}/${DIR}" | |
cd ${ROOT_DIR} | |
if [[ -z ${DIR} ]]; then | |
userInput | |
fi | |
if [[ ! -d ${DIR} ]]; then | |
echo -e "\nThis directory \"${DIR}\" doesn't exist. Please try again." | |
exit 0 | |
fi | |
} | |
function createOutputDir() { | |
OUTPUT_DIR="${INPUT_DIR}/OK" | |
if [[ ! -d ${OUTPUT_DIR} ]]; then | |
echo -e "\n> Creating directory \"OK\"..." | |
mkdir "${OUTPUT_DIR}" | |
else | |
echo -e "\nThe directory \"OK\" already exists. Please delete it before you run this script." | |
exit 0 | |
fi | |
} | |
function resizeAndWatermarkImages() { | |
MOGRIFY=$(command -v mogrify) | |
SIZE="1399" # Assumes W:931px x H:1399px | |
QUALITY="82" | |
WATERMARK="© WhateverIwantToPutHere" | |
COLOR="gray(40%)" | |
FONT="liberation-sans" | |
FONT_SIZE="42" | |
GRAVITY="East" | |
EXTENSION="jpg" | |
echo "> Resizing images and adding the watermark..." | |
${MOGRIFY} -path "${OUTPUT_DIR}"/ \ | |
-filter Triangle \ | |
-define filter:support=2 \ | |
-resize ${SIZE} \ | |
-unsharp 0.25x0.08+8.3+0.045 \ | |
-dither None \ | |
-posterize 136 \ | |
-quality ${QUALITY} \ | |
-define jpeg:fancy-upsampling=off \ | |
-define png:compression-filter=5 \ | |
-define png:compression-level=9 \ | |
-define png:compression-strategy=1 \ | |
-define png:exclude-chunk=all \ | |
-interlace none \ | |
-colorspace sRGB \ | |
-draw "rotate -90 text 440,40 '${WATERMARK}'" \ | |
-fill "${COLOR}" \ | |
-font ${FONT} \ | |
-pointsize ${FONT_SIZE} \ | |
-gravity ${GRAVITY} \ | |
"${INPUT_DIR}"/*.${EXTENSION} | |
echo -e "\nDone! Images are available in ${OUTPUT_DIR}" | |
} | |
userInput | |
createOutputDir | |
resizeAndWatermarkImages |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment