Skip to content

Instantly share code, notes, and snippets.

@elmimmo
Last active December 18, 2015 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elmimmo/5814243 to your computer and use it in GitHub Desktop.
Save elmimmo/5814243 to your computer and use it in GitHub Desktop.
Creates an image with a palete of swatches of the most common colors in an image, and their hexadecimal code, ordered by higher frequency from left to right.
#!/bin/bash
# Creates an image with a palete of swatches of the most common colors in an
# image, and their hexadecimal code, ordered by higher frequency from left to
# right.
#
# Requires Imagemagick.
#
# Author: Jorge Hernández Valiñani
# Usage message
USAGE="usage: [OPTION=\"value\"] palette.sh FILE
OUT_DIR (path) Output directory
SWATCH_SIZE (int) Size of the side of each square color swatch in px
SWATCH_NUMBER (int) Number of colors"
# Exit if anything breaks.
set -e
# Variables
: ${OUT_DIR:=${HOME}/Desktop/}
: ${SWATCH_SIZE:=100}
: ${SWATCH_NUMBER:=3}
# Boilerplate
# -----------
# If the script was ran without arguments, exit.
if [ -z "$1" ]; then
echo "$USAGE"
exit 1
fi
# Smart prompts
warn () { echo "$@" 1>&2; }
# Use the system's default temporary directory
: ${TMPDIR:="/tmp/"}
if [[ ! "$TMPDIR" =~ /$ ]]; then
TMPDIR=${TMPDIR}/
fi
# Check that some of the basic paths exist. Exit if not.
if [ ! -d "$TMPDIR" ]; then
warn "! Failed. Temporary directory '$TMPDIR' does not exist."
exit 1
fi
if [ ! -d "$OUT_DIR" ]; then
warn "! Failed. Output directory '$OUT_DIR' does not exist."
exit 1
fi
if [[ ! "$OUT_DIR" =~ /$ ]]; then
OUT_DIR=${OUT_DIR}/
fi
WORK_DIR=$(mktemp -d "${TMPDIR}${0##*/}".XXXXXXXXXX)/
# Cleanup temp files on exit (either on error or at the end of the script).
cleanup() {
rm -r "$WORK_DIR"
}
trap "cleanup" EXIT
# Tests
# -----
if [ ! -f "$1" ]; then
warn "! Failed. Could not find '$1'."
exit 1
fi
if [ -z "${SWATCH_SIZE##*[!0-9]*}" ]; then
warn "! Failed. SWATCH_SIZE is not a number."
exit 1
fi
if [ -z "${SWATCH_NUMBER##*[!0-9]*}" ]; then
warn "! Failed. SWATCH_NUMBER is not a number."
exit 1
fi
#Requires ImageMagick.
if [ ! -x "`type -P convert 2>&-`" ]; then
warn "! Failed. ImageMagick is not installed."
exit 1
fi
# Start
# -----
# Get ordered list of most common averaged colors
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
MY_COLOR+=( $(
convert "$1" -colors 16 -depth 8 -format "%c" histogram:info: \
| sort -rn \
| head -${SWATCH_NUMBER} \
| sed 's/^.*#/#/;s/ .*//;') )
IFS=$SAVEIFS
echo ${1##*/}: ${MY_COLOR[@]}
# Create image of color swatches
for (( i = 0; i < ${#MY_COLOR[*]}; i++ ))
do
echo "rectangle $(( $i * ${SWATCH_SIZE} )),0 $(( $i * ${SWATCH_SIZE} + ${SWATCH_SIZE} )),${SWATCH_SIZE}" \
> ${WORK_DIR}${1##*/}.$i.mvg
done
convert -size $(( ${#MY_COLOR[*]} * ${SWATCH_SIZE} ))x130 canvas: $(
for (( i = 0; i < ${#MY_COLOR[*]}; i++ ))
do
echo -n -fill "${MY_COLOR[i]}" -draw @${WORK_DIR}${1##*/}.$i.mvg \
-fill black -pointsize 20 -annotate 0x0+$(( 100 * $i +8 ))+120 "${MY_COLOR[i]}"
echo -n ' '
done) \
${OUT_DIR}${1##*/}_COLORS.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment