Is it possible to get the average image color with RMagick?

早过忘川 提交于 2019-11-27 11:20:31

问题


I need to know the average color from an image when I upload it to my Ruby on Rails application. Is it possible to get the average color value in HEX or in RGB to use this color later in the view that's going to display this image?

Something like:

img =  Magick::Image.read(path).first
hexVal = img.getHexValue

回答1:


Resize the image to one pixel and get its color?

img =  Magick::Image.read(path).first
pix = img.scale(1, 1)
averageColor = pix.pixel_color(0,0)



回答2:


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| avg[comp] /= 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| avg[comp] = (avg[comp] / 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.




回答3:


I found my solution (here), after I tested all the possibilities presented here.

def maincolor()
  img =  Magick::Image.read(self.url).first
  pix = img.scale(1, 1)
  avg_color_hex = pix.to_color(pix.pixel_color(0,0))
  return avg_color_hex
end

I hope this helps. I added the conversion to hex color by rmagick, because it's a pita with ruby ( otherwise I used sprintf to hex conversion)




回答4:


Consider using the miro gem, which seems to follow "mu is too short"'s approach: https://github.com/jonbuda/miro




回答5:


According to @muistooshort - If all the quantize function does is make a image less complex by taking averages of pixel colors (assuming) - wouldn't be even simpler if you just quantized the image down to color like:

img.quantize(1,Magick::RGBColorspace).color_histogram

And just use the resulting color?



来源:https://stackoverflow.com/questions/5162929/is-it-possible-to-get-the-average-image-color-with-rmagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!