Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Pixel Manipulation: These aren't my colors

I know questions like this have been asked several time, but I have yet to find just what I'm looking for. I am reading an image into a canvas object (in javascript) and trying to manipulate some specific pixels. For example, I am looking for the color RGB: 224 64 102, and trying to change this to a different color.

I can apply greyscale to the image, so I know the manipulation works, but the code is not finding any pixels with this color (that Adobe Illustrator said was the RGB color). I'm hoping I'm just missing a small detail. The code is below, hopefully someone will see it.

Thanks!

var canvas = document.getElementById("testcanvas");
            var canvasContext = canvas.getContext('2d');


            imgObj = new Image();
            imgObj.src = "ss.jpg";
            //imgObj.width = 200;
            //imgObj.height = 200;
            var imgW = imgObj.width;
            var imgH = imgObj.height;
            canvas.width = imgW;
            canvas.height = imgH;
            canvasContext.drawImage(imgObj, 0, 0);

            var imgPixels = canvasContext.getImageData(0, 0, imgW, imgH);

            //hash_table = {};

             for (var x = 0; x < imgPixels.width; x++) {
                for (var y = 0; y < imgPixels.height; y++)
                    {
                   var i = (y * imgPixels.width + x) * 4;
                     //Want to go from:
                     //E04066
                     //224 64 102 -> to
                     //134 135 185

                     if(imgPixels.data[i] == 224 && imgPixels.data[i+1] == 64 && imgPixels.data[i+2] == 102) {
                        imgPixels.data[i] = 134;
                        imgPixels.data[i+1] = 135;
                        imgPixels.data[i+2] = 185;
                     }

                     //To greyscale:
                     /*
                     var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
                     imgPixels.data[i] = avg;
                     imgPixels.data[i + 1] = avg;
                     imgPixels.data[i + 2] = avg;
                     imgPixels.data[i + 3] = 255;
                    */
                }
            }
            canvasContext.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
            //color_count = 0;
            //for(key in hash_table) {
            //  color_count++;
            //}
            //console.log(color_count);
            //console.log(hash_table);
            return canvas.toDataURL();

        });
});

</script>
</head>
<body>
    <canvas id="testcanvas"></canvas>

<img src="ss.jpg" id="testimage"/>
like image 626
The Green Pizza Avatar asked Feb 02 '26 06:02

The Green Pizza


1 Answers

You are probably unable to get image data from canvas because the canvas has been tainted by cross-origin data.

If that file, ss.jpg is local then it won't work. I imagine that's the case.

Search for canvas cross-origin on SO or Google for more information on that. There's a lot out there. Here's a bit of an explanation:

http://simonsarris.com/blog/480-understanding-the-html5-canvas-image-security-rules

Here's a site about enabling it on your server:

http://enable-cors.org/


Otherwise, your code works. Here is the same code converting a tiny red dot into a tiny green dot:

http://jsfiddle.net/RBaxt/

like image 69
Simon Sarris Avatar answered Feb 03 '26 19:02

Simon Sarris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!