Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.