-
-
Save parzibyte/cf092763635d6328441fd943d6fc3132 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void convertirImagenABlancoYNegro(Image imagen) { | |
var anchura = imagen.width; | |
var altura = imagen.height; | |
var umbralParaBlancoYNegro = 128; | |
for (var y = 0; y < altura; y++) { | |
for (var x = 0; x < anchura; x++) { | |
int r = getRed(imagen.getPixelSafe(x, y)); | |
int g = getGreen(imagen.getPixelSafe(x, y)); | |
int b = getBlue(imagen.getPixelSafe(x, y)); | |
int a = getAlpha(imagen.getPixelSafe(x, y)); | |
var gris = 0.3 * r + 0.59 * g + 0.11 * b; | |
if (gris > umbralParaBlancoYNegro) { | |
imagen.setPixelRgba(x, y, 255, 255, 255); // Blanco | |
} else { | |
// Nota: el alpha, entre más bajo, más transparente. O sea, alpha 0 es totalmente transparente, alpha 255 es opaco | |
if (a < umbralParaBlancoYNegro) { | |
imagen.setPixelRgba(x, y, 255, 255, 255); // Blanco | |
//imagen.setPixelRgba(x, y, 0, 0, 0); // Negro | |
} else { | |
imagen.setPixelRgba(x, y, 0, 0, 0); // Negro | |
} | |
} | |
} | |
} | |
File("blanco_y_negro.png").writeAsBytesSync(encodePng(imagen)); | |
} |
int r = getRed(imagen.getPixelSafe(x, y)); int g = getGreen(imagen.getPixelSafe(x, y)); int b = getBlue(imagen.getPixelSafe(x, y)); int a = getAlpha(imagen.getPixelSafe(x, y)); que son los metodos getRed getRed....
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
int r = getRed(imagen.getPixelSafe(x, y));
int g = getGreen(imagen.getPixelSafe(x, y));
int b = getBlue(imagen.getPixelSafe(x, y));
int a = getAlpha(imagen.getPixelSafe(x, y));
que son los metodos getRed getRed....