Skip to content

Instantly share code, notes, and snippets.

@orjanv
Created March 18, 2019 18:30
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 orjanv/edb5341fe4aa5429872f979fee56d7f8 to your computer and use it in GitHub Desktop.
Save orjanv/edb5341fe4aa5429872f979fee56d7f8 to your computer and use it in GitHub Desktop.
Identify dominant colors in an image
#!/bin/sh
# CROP IMAGE INTO HD FORMAT 1080X720 FROM CENTRE
convert $1 -resize 1080x1080 resized-$1
convert resized-$1 -gravity center -crop 1080x720 cropped-$1
# GET FIVE MOST DOMINANT HEX COLORS FROM ANY IMAGE
convert cropped-$1 +dither -colors 5 -unique-colors txt: | awk '{ print $3 }' | grep '#' > hex.tmp
# CREATE A RECTANGLE FROM EACH HEX COLOR WITH HEX CODE AS TEXT IN THE MIDDLE
while read p; do
convert -size 216x144 xc:$p rect-$p.png
convert -background '#0008' -fill white -gravity center -size 216x30 caption:$p rect-$p.png +swap -gravity south -composite rect-$p.png
done <hex.tmp
# COMBINE RECTANGLES INTO ONE COMBINED IMAGE
convert +append rect-*.png combined.png
# APPEND COMBINED IMAGE BELOW THE ORIGINAL IMAGE
convert -append cropped-$1 combined.png $1-colors.jpg
# REMOVE TEMPORARY FILES
rm hex.tmp rect* cropped-$1 resized-$1 #combined.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment