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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.”