How to find an average color of an image using JavaScript ? Last Updated : 14 Feb, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report The average color of an image can be found with JavaScript by drawing the image on a canvas element. The following steps have to be performed to get the average color of an image: 1. The image is first drawn on the canvas using the method context.drawImage() of the Canvas 2D API. This method takes in the image and the dimensions as parameters and draws it to the canvas. Syntax: context.drawImage( img, width, height ); 2. The image data of the canvas is returned using the context.getImageData() method. It returns an ImageData object representing the pixel data for a specified section of the canvas. Syntax: context.getImageData( x, y, width, height ) 3. The average red, green and blue colors are then calculated with this image data by adding all the color values separately and finding the average value of that color. Example: html <!DOCTYPE html> <html lang="en"> <head> <title> Find Average Color of image via JavaScript? </title> <style> #img { position: absolute; top: 20%; left: 25%; } #block { position: absolute; background-color: white; height: 70px; width: 70px; left: 50%; top: 25%; } </style> </head> <body> <img height="100px" width="150px" id="img" src= "image_to_find_average_color.png"> <div id="block"></div> <!-- Function to find the average color --> <script> function averageColor(imageElement) { // Create the canvas element var canvas = document.createElement('canvas'), // Get the 2D context of the canvas context = canvas.getContext && canvas.getContext('2d'), imgData, width, height, length, // Define variables for storing // the individual red, blue and // green colors rgb = { r: 0, g: 0, b: 0 }, // Define variable for the // total number of colors count = 0; // Set the height and width equal // to that of the canvas and the image height = canvas.height = imageElement.naturalHeight || imageElement.offsetHeight || imageElement.height; width = canvas.width = imageElement.naturalWidth || imageElement.offsetWidth || imageElement.width; // Draw the image to the canvas context.drawImage(imageElement, 0, 0); // Get the data of the image imgData = context.getImageData( 0, 0, width, height); // Get the length of image data object length = imgData.data.length; for (var i = 0; i < length; i += 4) { // Sum all values of red colour rgb.r += imgData.data[i]; // Sum all values of green colour rgb.g += imgData.data[i + 1]; // Sum all values of blue colour rgb.b += imgData.data[i + 2]; // Increment the total number of // values of rgb colours count++; } // Find the average of red rgb.r = Math.floor(rgb.r / count); // Find the average of green rgb.g = Math.floor(rgb.g / count); // Find the average of blue rgb.b = Math.floor(rgb.b / count); return rgb; } // Function to set the background // color of the second div as // calculated average color of image var rgb; setTimeout(() => { rgb = averageColor( document.getElementById('img')); // Select the element and set its // background color document .getElementById("block") .style.backgroundColor = 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ')'; }, 500) </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Create an Image Element using JavaScript? P parasmadan15 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to randomly change image color using HTML CSS and JavaScript ? In this article, we will create a webpage where we can change image color randomly by clicking on the image using HTML, CSS, and JavaScript. The image color will change randomly using the JavaScript Math random() function.Approach:First of all select your image using HTML <img> tag.In JavaScri 2 min read How to Create an Image Element using JavaScript? Creating an image element dynamically using JavaScript is a useful technique when you want to add images to a webpage without having to manually write the <img> tag in your HTML. This allows for more flexibility, as we can create and manipulate images based on user interactions or other condit 2 min read How to get the Width and Height of an Image using JavaScript? Given an image and the task is to get the width and height of the image using JavaScript. The width and height property is used to display the image width and height. Syntax for width:let width = this.width;Syntax for height:let height = this.height;Example 1: This example selects the image and then 2 min read How to pick a random color from an array using CSS and JavaScript ? The task is to pick a random color from an array using CSS. There is no way to do this using CSS only. CSS doesn't support logical statements because CSS is purely deterministic (there is nothing like array etc in CSS). We can use client-side JavaScript to select a random color from the array. Below 1 min read How to change color of PNG image using CSS? Given an image and the task is to change the image color using CSS. Use filter function to change the png image color. Filter property is mainly used to set the visual effect to the image. There are many property value exist to the filter function. filter: none|blur()|brightness()|contrast()|drop-sh 1 min read How to change text color depending on background color using JavaScript? The task is to set the foreground color based on the background color so that it will become visible. Here few of the important techniques are discussed. We are going to use JavaScript. Approach: First, select the random Background color(by selecting random RGB values) or a specific one.Use the YIQ 3 min read Like