是否可以使用 RMagick 获得平均图像颜色?

发布于 2024-10-19 17:39:35 字数 200 浏览 1 评论 0原文

当我将图像上传到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

偷得浮生 2024-10-26 17:39:35

将图像大小调整为一个像素并获取其颜色?

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

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)
凌乱心跳 2024-10-26 17:39:35

我认为你不能直接询问 RMagick 图像的平均颜色,但计算这样的事情并不困难。

我认为最简单的方法是提取 颜色直方图 然后使用来计算你的平均值。您可能想先量化图像,计算直方图对于具有多种颜色的图像来说,如果您只对平均值感兴趣,那么这并不便宜,而且可能是毫无意义的忙碌工作:

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 }

这将为您提供 avg 中的平均颜色。但是,颜色将采用 ImageMagick 的内部格式(即分量范围从零到 Magick::QuantumRange),因此您必须将它们缩小到 0-255:

[:r, :g, :b].each { |comp| avg[comp] = (avg[comp] / Magick::QuantumRange * 255).to_i }

最后您得到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:

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.

清风夜微凉 2024-10-26 17:39:35

在我测试了此处提出的所有可能性之后,我找到了我的解决方案(此处) 。

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

我希望这有帮助。我通过 rmagick 添加了到十六进制颜色的转换,因为它是带有 ruby​​ 的皮塔饼(否则我使用 sprintf 到十六进制转换)

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)

半边脸i 2024-10-26 17:39:35

考虑使用 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

趴在窗边数星星i 2024-10-26 17:39:35

根据@muistooshort - 如果所有量化函数所做的只是通过获取像素颜色的平均值(假设)来使图像变得不那么复杂 - 如果您只是将图像量化为如下颜色,则不会更简单:

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

并且只需使用结果颜色?

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?

情何以堪。 2024-10-26 17:39:35

Consider using the rails dominant colors gem, https://github.com/OpenGems/rails_dominant_colors

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文