Created
September 23, 2014 01:06
-
-
Save acrookston/b3ac8a21532385f7c39e to your computer and use it in GitHub Desktop.
Cross-posting from my Forrst: http://zurb.com/forrst/posts/Collecting_colours_from_an_image_Ruby-Lgo
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
require 'rmagick' | |
number_of_colors = 100 | |
file_path = "/path/to/file.jpg" | |
begin | |
temp_file = Magick::Image.read(file_path).first.quantize(number_of_colors) | |
pixels = {} | |
pixel_count = 0 | |
temp_file.each_pixel do |p,c,r| | |
pixel_count += 1 | |
pix = p.to_color(Magick::AllCompliance, false, 8) | |
pixels[pix] = (pixels[pix] || 0) + 1 | |
end | |
puts "Pixels: #{pixel_count}" | |
pixels.sort { |a,b| b[1] <=> a[1] } | |
rescue Magick::ImageMagickError | |
{} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment