Understanding how canvas converts an image to black and white

后端 未结 2 698
名媛妹妹
名媛妹妹 2021-01-21 02:01

I found this script for converting an image to black and white, which works great, but I was hoping to understand the code a little bit better. I put my questions in the code, i

2条回答
  •  后悔当初
    2021-01-21 02:57

    function grayscale(src){ //Creates a canvas element with a grayscale version of the color image
        //create canvas
        var canvas = document.createElement('canvas');
        //get its context
        var ctx = canvas.getContext('2d');
        //create empty image
        var imgObj = new Image();
        //start to load image from src url
        imgObj.src = src;
        //resize canvas up to size image size
        canvas.width = imgObj.width;
        canvas.height = imgObj.height;
        //draw image on canvas, full canvas API is described here http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html
        ctx.drawImage(imgObj, 0, 0);
        //get array of image pixels
        var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
        //run through all the pixels
        for(var y = 0; y < imgPixels.height; y++){
            for(var x = 0; x < imgPixels.width; x++){
                //here is x and y are multiplied by 4 because every pixel is four bytes: red, green, blue, alpha
                var i = (y * 4) * imgPixels.width + x * 4; //Why is this multiplied by 4?
                //compute average value for colors, this will convert it to bw
                var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
                //set values to array
                imgPixels.data[i] = avg; 
                imgPixels.data[i + 1] = avg; 
                imgPixels.data[i + 2] = avg;
            }
        }
        //draw pixels according to computed colors
        ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height); 
        return canvas.toDataURL();
    }
    

    In this function coefficient equal to 1/3 are used, however the usually used are: 0.3R + 0.59G + 0.11B (http://gimp-savvy.com/BOOK/index.html?node54.html).

提交回复
热议问题