Last active
February 8, 2021 18:00
-
-
Save tassaron/e1e2e2e683667d38a912818bd18b5ea3 to your computer and use it in GitHub Desktop.
wallpaper crop, resizer and watermark using ImageMagick
This file contains 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
#!/bin/bash | |
# wallpaper resizer and watermarker script | |
#=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~==~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~# | |
# by tassaron on march 5 2017 | |
# updated 2017-05-04, 2018-09-21, 2021-02-01 | |
# | |
#=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~==~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~# | |
# EDIT THESE VALUES | |
TMP_DIR="/tmp/mkwallpaper" | |
OUTPUT_DIR="${HOME}" | |
WATERMARK_IMG="${HOME}/Images/muffin/muffin-watermark.png" | |
WATERMARK_POSITION="SouthEast" | |
#RESOLUTION=1366x768 | |
RESOLUTION=1920x1080 | |
WATERMARK=0 | |
VERBOSE=0 | |
check_for() { | |
which "$1" &> /dev/null | |
if [ "$?" -ne 0 ]; then | |
echo "You need to install ImageMagick for ${1} command" | |
exit 1 | |
fi | |
} | |
check_for convert | |
check_for composite | |
# FUNCTIONS | |
create_resized_img() { | |
# create image with correct dimensions respecting aspect ratio, then cropped to size | |
if [ "$VERBOSE" = 1 ]; then echo "creating resized image for $(basename $new_img)..."; fi | |
convert -gravity center -resize $RESOLUTION^ -crop $RESOLUTION+0+0 $1 $2 | |
if [ $? -ne 0 ]; then | |
echo "Resizing ${1} failed" | |
exit 2 | |
fi | |
} | |
create_watermark_img() { | |
if ! [ -f "$WATERMARK_IMG" ]; then | |
echo "Watermark image is not accessible." | |
exit 3 | |
fi | |
if [ ! -e "${TMP_DIR}/resized/${new_img}" ]; then | |
create_resized_img "${new_img}" "${TMP_DIR}/resized/$(basename $new_img)" | |
fi | |
# add watermark to southeast corner and convert to jpg | |
if [ "$VERBOSE" = 1 ]; then echo "creating watermark image for $new_img..."; fi | |
composite -quality 90 -gravity $WATERMARK_POSITION "$WATERMARK_IMG" "${TMP_DIR}/resized/$(basename $new_img)" "$target" | |
if [ $? -ne 0 ]; then | |
echo "Compositing ${new_img} failed" | |
exit 4 | |
fi | |
} | |
main() { | |
# create watermarked resized wallpaper image | |
if [ "$WATERMARK" = 1 ]; then | |
target="${OUTPUT_DIR}/$(basename ${new_img})-watermark.jpg" | |
if [ -f "$target" ]; then | |
echo "${target} already exists. Not overwriting" | |
exit 5 | |
fi | |
if [ ! -e "$target" ]; then | |
create_watermark_img | |
fi | |
else | |
target="${TMP_DIR}/resized/$(basename ${new_img})" | |
if [ ! -e "$target" ]; then | |
create_resized_img "${new_img}" "$target" | |
fi | |
cp "$target" "${OUTPUT_DIR}/$(basename ${new_img})-resized.jpg" | |
if [ $? -ne 0 ]; then | |
echo "Copying ${new_img}-resized.jpg out of ${TMP_DIR} failed" | |
exit 9 | |
fi | |
fi | |
} | |
print_help () { | |
echo "usage: ${0} [--help] [--verbose] [--watermark /path/img.png] [--watermark-position SouthEast] [--res 1920x1080] input_filepath [-o output_dir]" | |
} | |
getting_res=0 | |
getting_output=0 | |
getting_watermark=0 | |
getting_watermark_pos=0 | |
pos_arg= | |
for argument; do | |
if [ "$getting_res" = 1 ]; then RESOLUTION="$argument"; getting_res=0; continue; fi | |
if [ "$getting_output" = 1 ]; then OUTPUT_DIR="$argument"; getting_output=0; continue; fi | |
if [ "$getting_watermark" = 1 ]; then WATERMARK_IMG="$argument"; getting_watermark=0; continue; fi | |
if [ "$getting_watermark_pos" = 1 ]; then WATERMARK_POSITION="$argument"; getting_watermark_pos=0; continue; fi | |
if [ "$argument" = "-h" ] || [ "$argument" = "--help" ]; then print_help; exit 0; fi | |
if [ "$argument" = "-v" ] || [ "$argument" = "--verbose" ]; then VERBOSE=1; continue; fi | |
if [ "$argument" = "-w" ] || [ "$argument" = "--watermark" ]; then getting_watermark=1; WATERMARK=1; continue; fi | |
if [ "$argument" = "-wpos" ] || [ "$argument" = "--watermark-position" ]; then getting_watermark_pos=1; continue; fi | |
if [ "$argument" = "-r" ] || [ "$argument" = "--res" ]; then getting_res=1; continue; fi | |
if [ "$argument" = "-o" ] || [ "$argument" = "--output" ]; then getting_output=1; continue; fi | |
pos_arg="$argument" | |
done | |
if [ -z "$pos_arg" ]; then | |
echo "Missing input filename (must be an image)" | |
echo | |
print_help | |
exit 6 | |
fi | |
if [ -d "$pos_arg" ]; then | |
echo "Skipping ${pos_arg}: Is a directory" | |
exit 7 | |
fi | |
# create directories if they don't exist | |
if ! [ -d "$TMP_DIR" ]; then | |
mkdir "$TMP_DIR" | |
if ! [ -d "$TMP_DIR" ]; then | |
echo "Failed to create $TMP_DIR" | |
exit 8 | |
fi | |
fi | |
if [ ! -d "${TMP_DIR}/resized" ]; then | |
mkdir "${TMP_DIR}/resized" | |
fi | |
new_img=$pos_arg | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment