Skip to content

Instantly share code, notes, and snippets.

@jyurek
Last active October 2, 2015 19:32
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 jyurek/fa68e90da05856e9c92a to your computer and use it in GitHub Desktop.
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.
#!/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`
#!/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}`
@jyurek
Copy link
Author

jyurek commented Oct 2, 2015

And a slightly updated on in ruby. This is ugly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment