Skip to content

Instantly share code, notes, and snippets.

@joshsmith
Created November 9, 2012 06:05
Show Gist options
  • Select an option

  • Save joshsmith/4043993 to your computer and use it in GitHub Desktop.

Select an option

Save joshsmith/4043993 to your computer and use it in GitHub Desktop.
Getting the 8 most frequent colors from an image
image = Magick::Image.read("https://s3.amazonaws.com/artburst-dev/artworks/medium/1/bubblegun.jpg").first
hist = image.quantize(8).color_histogram
pixels_array = []
pixels = hist.keys.sort_by {|pixel| hist[pixel]}
pixels.each { |pixel| pixels_array << [ pixel.to_color(Magick::AllCompliance,false,8), hist[pixel] ] }
pixels_array.reverse!
pixels_array[0] # ["#34689A", 42766]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment