-
-
Save maxkaplan/13bd70efe1265dd23d4479c53fd1fcbd to your computer and use it in GitHub Desktop.
A ruby script to get the most dominant colours in an image (uses ImageMagick)
This file contains hidden or 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 'open-uri' | |
require 'rubygems' | |
require 'rmagick' | |
include Magick | |
TOP_N = 10 # Number of swatches | |
# Create a 1-row image that has a column for every color in the quantized | |
# image. The columns are sorted decreasing frequency of appearance in the | |
# quantized image. | |
def sort_by_decreasing_frequency(img) | |
hist = img.color_histogram | |
# sort by decreasing frequency | |
sorted = hist.keys.sort_by {|p| -hist[p]} | |
new_img = Magick::Image.new(hist.size, 1) | |
new_img.store_pixels(0, 0, hist.size, 1, sorted) | |
end | |
def get_pix(img) | |
palette = Magick::ImageList.new | |
pixels = img.get_pixels(0, 0, img.columns, 1) | |
pixels.each do |p| | |
puts p.to_color(Magick::AllCompliance, false, 8, true) | |
end | |
end | |
image_url = "https://secure.gravatar.com/avatar/d0ed69be1d61caf4ddbdc74ce27788ff.png?s=200" | |
original = Magick::Image.from_blob(open(image_url).read).first | |
# reduce number of colors | |
quantized = original.quantize(TOP_N, Magick::RGBColorspace) | |
# Create an image that has 1 pixel for each of the TOP_N colors. | |
normal = sort_by_decreasing_frequency(quantized) | |
get_pix(normal) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment