了解画布如何将图像转换为黑色和白色
Understanding how canvas converts an image to black and white
我发现这个脚本用于将图像转换为黑色和白色,它工作得很好,但我希望更好地理解代码。我把我的问题放在代码中,以注释的形式。
谁能更详细地解释一下这里发生了什么?
function grayscale(src){ //Creates a canvas element with a grayscale version of the color image
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var imgObj = new Image();
imgObj.src = src;
canvas.width = imgObj.width;
canvas.height = imgObj.height;
ctx.drawImage(imgObj, 0, 0); //Are these CTX functions documented somewhere where I can see what parameters they require / what those parameters mean?
var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
for(var y = 0; y < imgPixels.height; y++){
for(var x = 0; x < imgPixels.width; x++){
var i = (y * 4) * imgPixels.width + x * 4; //Why is this multiplied by 4?
var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3; //Is this getting the average of the values of each channel R G and B, and converting them to BW(?)
imgPixels.data[i] = avg;
imgPixels.data[i + 1] = avg;
imgPixels.data[i + 2] = avg;
}
}
ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
return canvas.toDataURL();
}
-
画布函数和大多数函数一样,在官方规范中描述。此外,MDC对更"非正式"的文章也很有帮助。例如,MDC上的
drawImage
函数在这里 -
getImageData
函数返回一个对象,该对象包含一个包含所有像素字节数据的数组。每个像素用4个字节来描述:r
,g
,b
和a
。r
、g
和b
是颜色成分(红、绿、蓝),alpha是不透明度。所以每个像素使用4个字节,因此一个像素的数据从pixel_index * 4
开始。 -
是的,它在取平均值。因为在接下来的3行
所以基本上,对于所有像素,这将保持:r
,g
和b
都被设置为相同的值,所以您将获得每个像素的灰色(因为所有3个组件的数量是相同的)。r === g
,g === b
和r === b
。 为灰度(
0, 0, 0
为黑色,255, 255, 255
为白色)。function grayscale(src){ //Creates a canvas element with a grayscale version of the color image
//create canvas
var canvas = document.createElement('canvas');
//get its context
var ctx = canvas.getContext('2d');
//create empty image
var imgObj = new Image();
//start to load image from src url
imgObj.src = src;
//resize canvas up to size image size
canvas.width = imgObj.width;
canvas.height = imgObj.height;
//draw image on canvas, full canvas API is described here http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html
ctx.drawImage(imgObj, 0, 0);
//get array of image pixels
var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
//run through all the pixels
for(var y = 0; y < imgPixels.height; y++){
for(var x = 0; x < imgPixels.width; x++){
//here is x and y are multiplied by 4 because every pixel is four bytes: red, green, blue, alpha
var i = (y * 4) * imgPixels.width + x * 4; //Why is this multiplied by 4?
//compute average value for colors, this will convert it to bw
var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
//set values to array
imgPixels.data[i] = avg;
imgPixels.data[i + 1] = avg;
imgPixels.data[i + 2] = avg;
}
}
//draw pixels according to computed colors
ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
return canvas.toDataURL();
}
在这个函数中使用系数等于1/3,但通常使用的是:0.3R + 0.59G + 0.11B (http://gimp-savvy.com/BOOK/index.html?node54.html)。
相关文章:
- 使用jQuery以红色和黑色闪烁文本
- 将画布转换为pdf:黑色背景
- DT数据表背景颜色为黑色,但仍有悬停和选择颜色
- '返回'浏览器按钮返回白色页面
- 三js黑色材质,透明
- 黑色三件套帆布背景
- 在Firefox中导出高图表时出现黑色图像错误
- 谷歌图表显示空白白色页面
- Three.js-Skybox显示黑色
- 为什么我的犀牛书;s的SVG时钟为黑色圆圈
- Collada模型在Three.js中显示为黑色
- 如何将复选框(tickbox)的颜色从白色更改为黑色
- 根据十六进制背景获得白色/黑色
- 使用javascript创建10*10表,并使用从黑色到白色的颜色填充单元格
- 将选取器行标题从黑色更改为白色
- HCM(高对比度模式)白色与黑色检测
- 如何添加背景和文字的颜色分别为白色和黑色的导航在引导时,它被点击
- 了解画布如何将图像转换为黑色和白色
- 将导航条文本从白色变为黑色,就像装满水的杯子一样
- 您将如何仅使用Javascript(没有css)创建从黑色到白色的渐变