Is it posible to get the average image color with RMagick? - Ruby on Rails?

I don't think you can ask an RMagick image for its average color directly but computing such a thing isn't that difficult.

I don't think you can ask an RMagick image for its average color directly but computing such a thing isn't that difficult. I think the easiest way would be to extract the color histogram and then use that to compute your average. You'd probably want to quantize the image first though, computing the histogram for an image with a lot of colors is not cheap and probably pointless busy work if you're just interested in an average: total = 0 avg = { :r => 0.0, :g => 0.0, :b => 0.0 } img.quantize.

Color_histogram. Each { |c, n| avg:r += n * c. Red avg:g += n * c.

Green avg:b += n * c. Blue total += n } :r, :g, :b. Each { |comp| avgcomp /= total } That'll give you the average color in avg.

But, the color will be in ImageMagick's internal format (i.e. The components will range from zero to Magick::QuantumRange) so you'll have to scale them down to 0-255: :r, :g, :b. Each { |comp| avgcomp = (avgcomp / Magick::QuantumRange * 255).

To_i } And finally you have the RGB components in avg as integers between zero and 255 and getting the average color in hex format should be trivial. You could easily merge this into the averaging step if desired. I could probably be cleverer with the iterators but .

Each is nice and clear and clarity is more important than cleverness. You can also try with and without the quantization step and use whichever one works best for the images that you're working with.

Img = Magick::Image. Read(path). First pix = img.

Scale(1, 1) averageColor = pix. Pixel_color(0,0).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions