2

Does anyone know if it is possible to detect the "lightness" of an uploaded image using Ruby? I need to add border to those images that are too light, so they don't bleed into a light background.

Daniel
  • 245
  • 3
  • 10

1 Answers1

3

You'll probably end up using RMagick or a similar image manipulation library for this. First you have to decide which pixels you want to measure. Do you want the image as a whole, in which case it would make sense to average the colors of all the pixels and then compare it to some baseline lightness? Here's an example of that: Is it possible to get the average image color with RMagick? Alternatively you could just average all the pixels within, say, five pixels of the edge. This would be a very similar operation.

Once you've got an average pixel you can (in RMagick) use Pixel#to_hsla, which returns an array the third value of which is the HSL lightness and you can compare that against whatever threshold you choose for "light."

Community
  • 1
  • 1
Jordan Running
  • 102,619
  • 17
  • 182
  • 182