是否可以使用RMagick获得平均图像颜色? [英] Is it possible to get the average image color with RMagick?

查看:85
本文介绍了是否可以使用RMagick获得平均图像颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将图像上传到Ruby on Rails应用程序时,我需要知道图像的平均颜色。是否可以在HEX或RGB中获得平均颜色值,以便稍后在将要显示此图像的视图中使用此颜色?

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?

类似于:

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


推荐答案

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

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)

这篇关于是否可以使用RMagick获得平均图像颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆