Skip to content

Instantly share code, notes, and snippets.

@orjanv orjanv/hex-colors.sh
Created Mar 18, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.