Creating Black and White Image Using HTML5

Introduction

 
In this article, we learn how to create a Black and White image from a Color image using HTML5. When the user clicks on a button, the image changes from Color to Black and White and back again; when clicking again, the image changes back to the original.  
 
Step 1: First we define the body of the HTML using a canvas element, like this:
  1. <body>  
  2.    <div>  
  3.       <h1>Creating Black and White Image</h1>  
  4.       <input id='BnWButton' type='button' value="Click Me" onclick="return BnWButton_onclick()" />  
  5.    </div>  
  6.    <canvas id='canvas' width='800' height='750'>  
  7.       Sorry, Canvas is not supported in this Browser... 
  8.    </canvas>  
  9.    <script src='BnW.js' type="text/javascript"></script>  
  10. </body> 
Step 2: Then we define the functionality using JavaScript. We use the getImageData method to access pixels of the image and the setImageData method to put pixels back into the image. The Create_BnW function takes the average of the red, green, and blue values of each pixel and assigns that average to each of the values i.e. it drains the color from the image, like this:
  1. function Create_BnW() {  
  2.      var data = undefined,  
  3.  i = 0;  
  4.      imagedata = context.getImageData(0, 0, canvas.width, canvas.height);  
  5.      data = imagedata.data;  
  6.      for (i = 0; i < data.length - 4; i += 4) {  
  7.          average = (data[i] + data[i + 1] + data[i + 2]) / 3;  
  8.          data[i] = average;  
  9.          data[i + 1] = average;  
  10.          data[i + 2] = average;  
  11.      }  
  12.      context.putImageData(imagedata, 0, 0);  
  13.  }  
  14.  function Create_Color() {  
  15.      context.drawImage(image, 0, 0,image.width, image.height, 0, 0,context.canvas.width, context.canvas.height);  
  16.  }  
  17.  var a=0;  
  18.  function BnWButton_onclick() {  
  19.      if (a == 0) {  
  20.          Create_BnW();  
  21.          a++  
  22.      }  
  23.      else {  
  24.          Create_Color();  
  25.          a--;  
  26.      }  
  27.  }; 
Step 3: We use CSS in the Head section of the HTML for describing the look and formatting to our HTML page, like this:
  1. <head>  
  2.    <title>Create Black and White Image</title>  
  3.    <style> body {  
  4.       background: rgba(150, 205, 500, 0.3);  
  5.       }  
  6.       #canvas {  
  7.       margin: 10px 20px 0px 20px;  
  8.       border: thin solid Black;  
  9.       cursor: crosshair;  
  10.       }  
  11.       #BlacknWhite {  
  12.       width: 81px;  
  13.       }  
  14.    </style>  
  15. </head> 
The complete code looks like this:
  1. //HTML Code  
  2. <head>  
  3.    <title>Create Black and White Image</title>  
  4.    <style>  
  5.       body {  
  6.       background: rgba(150, 205, 500, 0.3);  
  7.       }  
  8.       #canvas {  
  9.       margin: 10px 20px 0px 20px;  
  10.       border: thin solid Black;  
  11.       cursor: crosshair;  
  12.       }  
  13.       #BlacknWhite {  
  14.       width: 81px;  
  15.       }  
  16.    </style>  
  17. </head>  
  18. <body>  
  19.    <div>  
  20.       <h1>Creating Black and White Image</h1>  
  21.       <input id='BnWButton' type='button' value="Click Me" onclick="return BnWButton_onclick()" />  
  22.    </div>  
  23.    <canvas id='canvas' width='800' height='750'>  
  24.       Sorry,Canvas is not supported in this Browser..  
  25.    </canvas>  
  26.    <script src='BnW.js' type="text/javascript"></script>  
  27. </body>  
  28. //JavaScript Code  
  29. var canvas = document.getElementById('canvas'),  
  30. context = canvas.getContext('2d'),  
  31. image = new Image(),  
  32. BnWButton = document.getElementById('BnWButton');  
  33. image.src = 'water.jpg';  
  34. image.onload = function () {  
  35. context.clearRect(0, 0, canvas.width, canvas.height);  
  36. context.drawImage(image, 0, 0, image.width, image.height, 0, 0, context.canvas.width, context.canvas.height);  
  37. };  
  38. function Create_BnW() {  
  39. var data = undefined,  
  40. i = 0;  
  41. imagedata = context.getImageData(0, 0, canvas.width, canvas.height);  
  42. data = imagedata.data;  
  43. for (i = 0; i < data.length - 4; i += 4) {  
  44. average = (data[i] + data[i + 1] + data[i + 2]) / 3;  
  45. data[i] = average;  
  46. data[i + 1] = average;  
  47. data[i + 2] = average;  
  48. }  
  49. context.putImageData(imagedata, 0, 0);  
  50. }  
  51. function Create_Color() {  
  52. context.drawImage(image, 0, 0,image.width, image.height, 0, 0,context.canvas.width, context.canvas.height);  
  53. }  
  54. var a=0;  
  55. function BnWButton_onclick() {  
  56. if (a == 0) {  
  57. Create_BnW();  
  58. a++  
  59. }  
  60. else {  
  61. Create_Color();  
  62. a--;  
  63. }  
  64. }; 
Output
 
BnW1.jpg
 
After button click
 
BnW2.jpg
 
When clicking again, the image switches back to the original color
 
 BnW3