Сделать белые пиксели на холсте прозрачными? - PullRequest
1 голос
/ 06 марта 2012

я пытаюсь создать скрипт, который меняет цвет изображений, я нашел способ изменить цвет, но так как изображения имеют белый фон, это меняет ВСЕ изображения, возможно ли как-то игнорировать белые пиксели?

Это код на данный момент:

    <!doctype html>
<html>
<head>

</head>

<body>
<h4>Original Image</h4>
<img id="testImage" src='../src/test.png'/>

<h4>Image copied to canvas</h4>
<canvas id="canvas" width="500" height="500"></canvas>

<h4>Modified Image copied to an image tag</h4>
<img id="imageData"/>

<script>

var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d"),
image = document.getElementById("testImage");

ctx.drawImage(image,0,0);

var imgd = ctx.getImageData(0, 0, 128, 128),
    pix = imgd.data,
    uniqueColor = [0,0,150]; // Blue for an example, can change this value to be anything.

// Loops through all of the pixels and modifies the components.
for (var i = 0, n = pix.length; i <n; i += 4) {
      pix[i] = uniqueColor[0];   // Red component
      pix[i+1] = uniqueColor[1]; // Blue component
      pix[i+2] = uniqueColor[2]; // Green component
      //pix[i+3] is the transparency.
}

ctx.putImageData(imgd, 0, 0);


var savedImageData = document.getElementById("imageData");
savedImageData.src = canvas.toDataURL("image/png");
</script>
</body>
</html>

1 Ответ

2 голосов
/ 06 марта 2012

Хотелось бы что-нибудь подобное для вас?

    if (pix[i] == 255 &&  
        pix[i+1] == 255 &&
        pix[i+2] == 255)
    {
    //pixel is white
    }
    else
    {
    //pixel is not white, modify it.
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...