Last active
October 2, 2015 19:32
-
-
Save jyurek/fa68e90da05856e9c92a to your computer and use it in GitHub Desktop.
A small bash script for outputting the most used 5 colors in an image.
This file contains 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/bash | |
for c in $(convert $1 -colors 5 -depth 8 -format %c histogram:info: | sort -d -r | cut -d '#' -f 2 | cut -d ' ' -f 1); do | |
colors+=($c) | |
convert -size 50x50 'canvas:#'$c /tmp/$c.png | |
done | |
color_files= | |
for c in ${colors[@]}; do | |
color_files="$color_files /tmp/$c.png" | |
done | |
convert \( $color_files +append \) `basename $1.output.png` |
This file contains 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
#!/usr/bin/env ruby | |
rows = 2 | |
colors = 10 | |
file = ARGV[0] | |
command = %Q{convert "#{file}" -colors #{colors} -depth 8 -format %c histogram:info: | sort -d -r | cut -d '#' -f 2 | cut -d ' ' -f 1} | |
hist = `#{command}`.split("\n") | |
def generate_line(filenames) | |
'\( ' + filenames.join(" ") + ' +append \) ' | |
end | |
color_files = hist.map do |color| | |
color_file = "/tmp/#{color}.png" | |
`convert -size 50x50 'canvas:#'#{color} #{color_file}` | |
color_file | |
end | |
output_file = "#{File.basename(file)}.palette.png" | |
lines = [] | |
per_row = (colors / rows) | |
rows.times do |x| | |
min = per_row * x | |
max = per_row * (x+1) | |
lines << generate_line(color_files[min...max]) | |
end | |
`convert #{lines.join(" ")} -append #{output_file}` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And a slightly updated on in ruby. This is ugly!