Sha256: 28910474b77f536a598f4485c52cb339061f497da9b52f884955109b70658b4c

Contents?: true

Size: 1.31 KB

Versions: 38

Compression:

Stored size: 1.31 KB

Contents

#! /usr/local/bin/ruby -w

require 'RMagick'
include Magick

NUM_COLORS = 256
HIST_HEIGHT = 200

img = Image.read('images/Hot_Air_Balloons_H.jpg').first
img = img.quantize(NUM_COLORS)

begin
    hist = img.color_histogram
rescue NotImplementedError
    img = Image.read('images/notimplemented.gif').first
    img.write('color_histogram.gif')
    exit
end

# sort pixels by increasing count
begin
    pixels = hist.keys.sort_by {|pixel| hist[pixel] }
rescue NameError    # No sort_by?
    pixels = hist.keys.sort { |p, q| hist[p] <=> hist[q] }
end

scale = HIST_HEIGHT / (hist.values.max*1.025)   # put 2.5% air at the top

gc = Draw.new
gc.stroke_width(1)
gc.affine(1, 0, 0, -scale, 0, HIST_HEIGHT)

# handle images with fewer than NUM_COLORS colors
start = NUM_COLORS - img.number_colors

pixels.each { |pixel|
    gc.stroke(pixel.to_color)
    gc.line(start, 0, start, hist[pixel])
    start = start.succ
}

hatch = HatchFill.new("white", "gray95")
canvas = Image.new(NUM_COLORS, HIST_HEIGHT, hatch)
gc.draw(canvas)

text = Draw.new
text.annotate(canvas, 0, 0, 0, 20, "Color Frequency\nHistogram") {
    self.pointsize = 10
    self.gravity = NorthGravity
    self.stroke = 'transparent'
    }

canvas.border!(1, 1, "white")
canvas.border!(1, 1, "black")
canvas.border!(3, 3, "white")
#canvas.display
canvas.write("color_histogram.gif")

exit

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
rmagick-1.10.0 doc/ex/color_histogram.rb
rmagick-1.10.1 doc/ex/color_histogram.rb
rmagick-1.12.0 doc/ex/color_histogram.rb
rmagick-1.13.0 doc/ex/color_histogram.rb
rmagick-1.11.0 doc/ex/color_histogram.rb
rmagick-1.11.1 doc/ex/color_histogram.rb
rmagick-1.15.0 doc/ex/color_histogram.rb
rmagick-1.15.10 doc/ex/color_histogram.rb
rmagick-1.14.0 doc/ex/color_histogram.rb
rmagick-1.14.1 doc/ex/color_histogram.rb
rmagick-1.15.1 doc/ex/color_histogram.rb
rmagick-1.15.13 doc/ex/color_histogram.rb
rmagick-1.15.15 doc/ex/color_histogram.rb
rmagick-1.15.11 doc/ex/color_histogram.rb
rmagick-1.15.12 doc/ex/color_histogram.rb
rmagick-1.15.14 doc/ex/color_histogram.rb
rmagick-1.15.2 doc/ex/color_histogram.rb
rmagick-1.15.4 doc/ex/color_histogram.rb
rmagick-1.15.16 doc/ex/color_histogram.rb
rmagick-1.15.17 doc/ex/color_histogram.rb