Created
September 22, 2009 00:24
-
-
Save 13k/190657 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/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