-2

Is there any way to convert the image so it appears as a gray scale. Changing rgb is not working as required.

VDP
  • 6,128
  • 4
  • 27
  • 52
user1079065
  • 1,717
  • 5
  • 27
  • 46

5 Answers5

0

If you're able to use HTML5 you could use a canvas, as seen here.

Jesse van Assen
  • 2,124
  • 2
  • 15
  • 18
0

Google for "canvas grayscale image" the first link will be:

HTML 5 introduces Canvas object which can be used to draw and manipulate images. In this example, I used a Canvas to turn an image into grayscale. If your browser supports Canvas, you should see two images above: the normal image and the grayscale one. The grayscale one is not a separate image, it is generated by utilizing Canvas.

http://permadi.com/tutorial/jsCanvasGrayscale/index.html

jantimon
  • 33,244
  • 22
  • 110
  • 172
0

If you want this to be done client side (on the browser) with a HTML you will need to use a Canvas. Example here: http://www.youtube.com/watch?v=QJa7tWScXS4

Ross
  • 13,412
  • 11
  • 57
  • 88
0

Use PaintbrushJS:

http://mezzoblue.github.com/PaintbrushJS/demo/

It lets you apply any number of filters including greyscale and works in most modern browsers. It's not a 'HTML' solution, that would not be possible without two images.

At least it works on most browsers, including ones which don't yet support HTML5.

deed02392
  • 4,478
  • 2
  • 26
  • 48
0

As you probably know, screen color consists of 3 components: red, green and blue. Each component or color (for example, red) has a value from 0 to 255. The value 0 indicates there is no red color and the value 255 would be the brightest possible red color. To convert a color to grayscale you just need to calculate the average for all three components. This can be done using the simple formula below:

grayscalecolor = (red + green + blue) / 3;

Canvas Method

var canvas = document.createElement('canvas');

var canvasContext = canvas.getContext('2d');

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);

for(>var y = 0; y < imgPixels.height; y++){
     for(>var x = 0; x < imgPixels.width; x++){
          var i = (y * 4) * imgPixels.width + x * 4;
          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;
     }
}
canvasContext.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);

return canvas.toDataURL();

Microsoft Internet Explorer does not support the CANVAS tag, but Microsoft does provide image manipulation through filters. The filter to use for converting images to grayscale is the filter named DXImageTransform.Microsoft.BasicImage.

imgObj.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(grayScale=1)';