Image brightness detection in client side script
Does anyone know if there is a script available to detect darkness/lightness in an image (HTML included) using a client sided script?
I basically want to be able to detect the brightness of image (dark/light) used in the background and have CSS/HTML/jQuery/JS adapt the page based on a variable that is either dark or light (true or false).
I know there are server-side scripts available but cannot use that for this particular development project.
Solution 1:
This function will convert each color to gray scale and return average of all pixels, so final value will be between 0 (darkest) and 255 (brightest)
function getImageLightness(imageSrc,callback) {
var img = document.createElement("img");
img.src = imageSrc;
img.style.display = "none";
document.body.appendChild(img);
var colorSum = 0;
img.onload = function() {
// create canvas
var canvas = document.createElement("canvas");
canvas.width = this.width;
canvas.height = this.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(this,0,0);
var imageData = ctx.getImageData(0,0,canvas.width,canvas.height);
var data = imageData.data;
var r,g,b,avg;
for(var x = 0, len = data.length; x < len; x+=4) {
r = data[x];
g = data[x+1];
b = data[x+2];
avg = Math.floor((r+g+b)/3);
colorSum += avg;
}
var brightness = Math.floor(colorSum / (this.width*this.height));
callback(brightness);
}
}
Usage:
getImageLightness("image.jpg",function(brightness){
console.log(brightness);
});
JSFiddle:
http://jsfiddle.net/s7Wx2/
Solution 2:
My answer reuses most of the code in @lostsource's answer but it uses a different method to attempt to distinguish between dark and light images.
First we need to (briefly) analyze what is the result of the average value of the sum of the RGB channels. For humans, it is meaningless. Is pink brighter than green? I.e., why would you want (0, 255, 0) to give a lower brightness value than (255, 0, 255)? Also, is a mid gray (128, 128, 128) bright just like a mid green (128, 255, 0)? To take this into consideration, I only deal with the color brightness of the channel as is done in the HSV color space. This is simply the maximum value of a given RGB triplet.
The rest is heuristics. Let max_rgb = max(RGB_i)
for some point i
. If max_rgb
is lower than 128 (assuming a 8bpp image), then we found a new point i
that is dark, otherwise it is light. Doing this for every point i
, we get A
points that are light and B
points that are dark. If (A - B)/(A + B) >= 0
then we say the image is light. Note that if every point is dark, then you get a value of -1, conversely if every point is light you get +1. The previous formula can be tweaked so you can accept images barely dark. In the code I named the variable as fuzzy
, but it does no justice to the fuzzy
field in Image Processing. So, we say the image is light if (A - B)/(A + B) + fuzzy >= 0
.
The code is at http://jsfiddle.net/s7Wx2/328/, it is very straightforward, don't let my notations scare you.
Solution 3:
A script called Background Check can detect the darkness/lightness in an image. It uses JavaScript to do so.
Here is a link to it:
http://www.kennethcachia.com/background-check/
I hope that helps anyone wanting to make a slider with this type of detection within it.
Solution 4:
MarvinJ provides the method averageColor(image) to get the average color of a given image. Having the average color, you can create rules to define the color of the label over the image.
Loading an image:
var image = new MarvinImage();
image.load("https://i.imgur.com/oOZmCas.jpg", imageLoaded);
Getting the average color:
var averageColor = Marvin.averageColor(image2); // [R,G,B]
The output of the snippet of this post:
var canvas = document.getElementById("canvas");
var image1 = new MarvinImage();
image1.load("https://i.imgur.com/oOZmCas.jpg", imageLoaded);
var image2 = new MarvinImage();
image2.load("https://i.imgur.com/1bZlwv9.jpg", imageLoaded);
var loaded=0;
function imageLoaded(){
if(++loaded == 2){
var averageColor;
averageColor = Marvin.averageColor(image1);
setText("LION", averageColor, "text1");
averageColor = Marvin.averageColor(image2);
setText("LION", averageColor, "text2");
}
}
function setText(text, averageColor, id){
if(averageColor[0] <= 80 && averageColor[1] <= 80 && averageColor[2] <= 80){
document.getElementById(id).innerHTML = "<font color='#ffffff'>"+text+"</font>";
}
else if(averageColor[0] >= 150 && averageColor[1] >= 150 && averageColor[2] >= 150){
document.getElementById(id).innerHTML = "<font color='#000000'>"+text+"</font>";
}
}
.divImage{
width:400px;
height:268px;
display:grid;
}
.divText{
font-family:Verdana;
font-size:56px;
font-weight:bold;
margin:auto;
display:table-cell;
}
<script src="https://www.marvinj.org/releases/marvinj-0.8.js"></script>
<div id="result"></div>
<div style="background-image:url(https://i.imgur.com/oOZmCas.jpg);" class="divImage">
<div id="text1", class="divText"></div>
</div>
<div style="background-image:url(https://i.imgur.com/1bZlwv9.jpg);" class="divImage">
<div id="text2", class="divText"></div>
</div>