Skip to content

Instantly share code, notes, and snippets.

@velenux
Last active November 9, 2018 00:54
Show Gist options
  • Save velenux/bd0b3dfa92695d6819d71a86df4fa7a7 to your computer and use it in GitHub Desktop.
Save velenux/bd0b3dfa92695d6819d71a86df4fa7a7 to your computer and use it in GitHub Desktop.
Create a 1080p and a 2160p images from a starting image and apply a watermark
#!/bin/bash
# iterates over all the .jpg files in a directory
# * creates a 1080p version with a 50% transparent signature (if not already present)
# * creates a 2160p version with a 8% transparent signature (if not already present)
FILE_sign_1080=signature_1080.png
FILE_sign_2160=signature_2160.png
# check parameters
if [ -z "$1" ]; then
echo "Use: $0 <directory_containing_JPGs>"
exit 1
fi
# remove trailing slash
target_dir="${1%/}"
# make sure it is a valid directory
if [ ! -d "$target_dir" ]; then
echo "Use: $0 <directory_containing_JPGs>"
exit 1
fi
# change the Input Field Separator to handle files with spaces
OLD_IFS=$IFS
IFS="$(printf '\n\t')"
# create the destination directories
mkdir "${target_dir}/1080px" "${target_dir}/2160px"
# iterate over the files
for target in ${target_dir}/*.jpg ; do
echo "Working on $target ..."
# we want to use a different command for portrait-oriented images
imagesize=$(identify -format '%wx%h' "$target")
orientation=""
w=$(echo $imagesize|cut -dx -f1)
h=$(echo $imagesize|cut -dx -f2)
if [ $h -gt $w ]; then
orientation="portrait"
fi
# we need this to build the resized file name
target_name=$(basename "$target" .jpg)
target_1080="${target_dir}/1080px/${target_name}_1080.jpg"
target_2160="${target_dir}/2160px/${target_name}_2160.jpg"
# create the 1080 pixel image
if [ ! -f "${target_1080}" ]; then
echo " - Creating the 1080 pixel image"
if [ "x${orientation}" == "xportrait" ]; then
convert $target -filter lanczos2 -resize "x1080>" -quality 91 tempfile.jpg
else
convert $target -filter lanczos2 -resize "1080^>" -quality 91 tempfile.jpg
fi
echo " - Signing the 1080 pixel image"
composite -dissolve 50% -gravity SouthEast ${FILE_sign_1080} tempfile.jpg ${target_1080}
fi
sync
# create the 2160 pixel image
if [ ! -f "${target_2160}" ]; then
echo " - Creating the 2160 pixel image"
if [ "x${orientation}" == "xportrait" ]; then
convert $target -filter lanczos2 -resize "x2160>" -quality 96 tempfile.jpg
else
convert $target -filter lanczos2 -resize "2160^>" -quality 96 tempfile.jpg
fi
echo " - Signing the 2160 pixel image"
composite -dissolve 8% -gravity SouthEast ${FILE_sign_2160} tempfile.jpg ${target_2160}
fi
sync
done
IFS=$OLD_IFS
rm -f tempfile.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment