Rmagick histogram problems
I'm having problems accessing the hash returned by color_histogram(). There are only black and white pixels in my image, and this is very frustrating.
When I do
puts hist.inspect() # returns "{red=0, green=0, blue=0, opacity=0=>779753, red=65535, green=65535, blue=65535, opacity=开发者_运维知识库0=>6679}"
According to the documentation, the key is a Pixel object, so I construct
black = Magick::Pixel.from_color('black')
white = Magick::Pixel.from_color('white')
puts black.inspect # red=0, green=0, blue=0, opacity=0
puts white.inspect # red=65535, green=65535, blue=65535, opacity=0
puts hist[white] # raises exception: `[]': can't convert Magick::Pixel into Integer (TypeError)
Any ideas how I can access the histogram easily?
Thanks
You're changing hist
somewhere between your color_histogram
call and your hist[white]
. This works for me with a simple black and white bw.png
:
> i = Magick::Image.read('bw.png').first
=> bw.png PNG 100x100 100x100+0+0 DirectClass 8-bit 466b
> h = i.color_histogram
=> {red=0, green=0, blue=0, opacity=0=>2342, red=65535, green=65535, blue=65535, opacity=0=>7658}
> black = Magick::Pixel.from_color('black')
=> red=0, green=0, blue=0, opacity=0
> h[black]
=> 2342
And you "can't convert Magick::Pixel into Integer (TypeError)" error is easily reproduced using an Array:
> a = [ ]
=> []
> a[black]
TypeError: can't convert Magick::Pixel into Integer
A Hash will never produce that TypeError from a simple access.
精彩评论