Pragmatism in the real world

Darkening an image with ImageMagick

I regularly need to darken images for background use behind a title. I’ve been using a filter in Acorn, but finally decided to make it a script that uses ImageMagick so that I could simplify it all with Alfred. This is the script:

darken-image.sh

#!/usr/bin/env bash

if [ -z "$1" ]
then
  echo "Usage: darken-image  [contrast=40]"
  echo ""
  exit 1
fi

in_filename="$1"
amount=${2:-40}

# Set output filename to original filename with "-darker" appended
extension="${in_filename##*.}"
name="${in_filename%.*}"
out_filename="${name}-darker.${extension}"

# Use ImageMagick to darken the original image and create the new file
convert "$in_filename" -fill black -colorize "$amount"% "$out_filename"

echo "Darkened image created: $out_filename"

It’s not very complicated, though I did have to look up Bash’s parameter expansion again as I always forget it!

The actual image processing with ImageMagick’s convert application using colorize operator to blend the black fill colour into the image, which results in a darker image. I set a default of 40, but this can be changed on the command line as some images need a different value.

Little automation scripts like this make life much easier.