简体   繁体   中英

Javascript - how to greyscale an image using this code?

I'm having another problem with the Image processing app. There's calculations in my workbook:

f(x.y) = (max(R, G, B) + min(R, G, B)) / 2

And that's for the average for the most prominent and least prominent colours.

f(x.y) = (0.21 R + 0.72 G + 0.07 B)

And that's for the Luminosity, weighted average.

f(x.y) = R + G + B) / 3

And that's for the average, but I've done that already.

Here's my code again. I'm sorry about this.

var mySrcImg = new Image ();
mySrcImg.crossOrigin = "";
mySrcImg.onload = test;
mySrcImg.src = "Your image";
function test () {
    var canvas = document.getElementById("canvas");
    var context = canvas.getContext("2d");                  
    context.fillStyle = "rgb(0,200,0)";
    context.fillRect (0, 0, 800, 800);
    // Copy the image to the canvas
    context.drawImage (mySrcImg, 0, 0);
    // Get your image data
    var myImageData;
    myImageData = context.getImageData(0, 0, canvas.width, canvas.height);                  
    // Loop over each pixel and invert the colours
    var pixelComponents = myImageData.data;
    var n = pixelComponents.length;
    for (var i = 0; i < n; i += 4) {
    // One pixel:
                            var average = (Math.max(pixelComponents[i  ], pixelComponents[i+1],
                                            pixelComponents[i+2]) + Math.min(pixelComponents[i  ], 
                                            pixelComponents[i+1], pixelComponents[i+2])) / 2;
    pixelComponents[i  ]; // red
    pixelComponents[i+1]; // green
    pixelComponents[i+2]; // blue
    // i+3 is alpha (the fourth element)
}
                        
// Draw the ImageData object at the given (x,y) coordinates 
context.putImageData(myImageData, 0, 0);
document.getElementById("srcImgDiv").appendChild(mySrcImg);
}
!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>My Image Processing App</title>
    </head>
    
    <body> 
        <h1 id="MyHeader">Image Processing App</h1>
        <p><b>Original Image:</b></p>
            <div id='srcImgDiv'></div>
        
            <p><br></p>
        <p><b>Converted Image:</b></p>
            <canvas id="canvas" width="800" height="800"> </canvas>
            <p><br></p> 

My question is: how can I make it greyscal? They've put:

var average = (Math.max(pixelComponents[i  ], pixelComponents[i+1],
                                            pixelComponents[i+2]) + Math.min(pixelComponents[i  ], 
                                            pixelComponents[i+1], pixelComponents[i+2])) / 2;

For the greyscaling.

Any help would be greatly appreciated.

Thanks.

If you want to easily grayscale an image, you could just use CSS. It already provides a filter: grayscale() for you. See MDN .

If you can't use CSS, you should take a look at Jimp (JavaScript Image Manipulation Program) . This lets you do anything from grayscale to inversion to anything else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM