Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created October 12, 2022 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/cf092763635d6328441fd943d6fc3132 to your computer and use it in GitHub Desktop.
Save parzibyte/cf092763635d6328441fd943d6fc3132 to your computer and use it in GitHub Desktop.
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));
}
@victorcode1
Copy link

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....

@parzibyte
Copy link
Author

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....

https://www.google.com/search?q=%22getRed%22+dart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment