Skip to content

Instantly share code, notes, and snippets.

@13k
Created September 22, 2009 00:24
Show Gist options
  • Select an option

  • Save 13k/190657 to your computer and use it in GitHub Desktop.

Select an option

Save 13k/190657 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Identifies the predominant color in an image.
# You can specify certain colors to discard from the result.
#
# Usage: $0 <image> [colors to discard]
[ -z "$1" ] && exit 1
file="$1"
shift
COLORS="$@"
filter_colors() {
while read color; do
out=false
for c in $COLORS; do
echo "$color" | grep -q "$c" && out=true && break
done
$out && continue || echo "$color"
done
}
identify -verbose "$file" | awk \
'BEGIN { parse=0 }
/^ Histogram:/ { parse=1; next }
/^ +[0-9]+:/ {
if (parse == 1) {
print gensub(/[ ]+([0-9]+):.+(#[A-F0-9]+).+/, "\\1 \\2", 1)
}
}' | sort -n -r | filter_colors | head -1 | awk '{print $2}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment