Skip to content

Instantly share code, notes, and snippets.

@acrookston
Created September 23, 2014 01:06
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 acrookston/b3ac8a21532385f7c39e to your computer and use it in GitHub Desktop.
Save acrookston/b3ac8a21532385f7c39e to your computer and use it in GitHub Desktop.
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