是否可以使用 RMagick 获得平均图像颜色?
当我将图像上传到 Ruby on Rails 应用程序时,我需要知道图像的平均颜色。是否可以获取十六进制或 RGB 形式的平均颜色值,以便稍后在要显示该图像的视图中使用该颜色?
像这样的东西:
img = Magick::Image.read(path).first
hexVal = img.getHexValue
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将图像大小调整为一个像素并获取其颜色?
Resize the image to one pixel and get its color?
我认为你不能直接询问 RMagick 图像的平均颜色,但计算这样的事情并不困难。
我认为最简单的方法是提取 颜色直方图 然后使用来计算你的平均值。您可能想先量化图像,计算直方图对于具有多种颜色的图像来说,如果您只对平均值感兴趣,那么这并不便宜,而且可能是毫无意义的忙碌工作:
这将为您提供
avg
中的平均颜色。但是,颜色将采用 ImageMagick 的内部格式(即分量范围从零到Magick::QuantumRange
),因此您必须将它们缩小到 0-255:最后您得到
avg
中的 RGB 分量为 0 到 255 之间的整数,并且以十六进制格式获取平均颜色应该很简单。如果需要,您可以轻松地将其合并到平均步骤中。我可能可以更聪明地使用迭代器,但是
.each
很好而且清晰,清晰比聪明更重要。您还可以尝试使用或不使用量化步骤,并使用最适合您正在处理的图像的一种。
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:
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 toMagick::QuantumRange
) so you'll have to scale them down to 0-255: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.
在我测试了此处提出的所有可能性之后,我找到了我的解决方案(此处) 。
我希望这有帮助。我通过 rmagick 添加了到十六进制颜色的转换,因为它是带有 ruby 的皮塔饼(否则我使用 sprintf 到十六进制转换)
I found my solution (here), after I tested all the possibilities presented here.
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)
考虑使用 miro gem,它似乎遵循“mu 太短”的做法:
https://github.com/jonbuda/miro
Consider using the miro gem, which seems to follow "mu is too short"'s approach:
https://github.com/jonbuda/miro
根据@muistooshort - 如果所有量化函数所做的只是通过获取像素颜色的平均值(假设)来使图像变得不那么复杂 - 如果您只是将图像量化为如下颜色,则不会更简单:
并且只需使用结果颜色?
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:
And just use the resulting color?
考虑使用 Rails 主色 gem,https://github.com/OpenGems/rails_dominant_colors
Consider using the rails dominant colors gem, https://github.com/OpenGems/rails_dominant_colors