Can't seem to save a transparent PNG
in
Programming Questions
•
1 year ago
Hi all...
I have some simple code that grabs an image, finds every pixel that's more blue than red or green, and turns those pixels transparent. That all seems to work. But I also want to save a new PNG that includes those transparent pixels. That part doesn't seem to work -- the output PNG is just solid black. What am I doing wrong here?
Thanks in advance!
void setup() {
int imageWidth = 2048;
int imageHeight = 1216;
color clear = color(0x00000000);
size(imageWidth,imageHeight);
PGraphics outputImage;
outputImage = createGraphics(imageWidth, imageHeight, P2D);
PImage testInputImage = new PImage();
testInputImage = loadImage("imputImage.png");
outputImage.beginDraw();
for (int i = 0; i < imageWidth; i++) {
for (int j = 0; j < imageHeight; j++) {
color thisPoint = testInputImage.get(i,j);
if ((blue(thisPoint) >= green(thisPoint)) && (blue(thisPoint) >= red(thisPoint))) {
stroke(clear);
} else {
stroke(testInputImage.get(i, j));
}
point(i,j);
outputImage.point(i,j);
}
}
outputImage.endDraw();
outputImage.save("outputImage.png");
}
I have some simple code that grabs an image, finds every pixel that's more blue than red or green, and turns those pixels transparent. That all seems to work. But I also want to save a new PNG that includes those transparent pixels. That part doesn't seem to work -- the output PNG is just solid black. What am I doing wrong here?
Thanks in advance!
void setup() {
int imageWidth = 2048;
int imageHeight = 1216;
color clear = color(0x00000000);
size(imageWidth,imageHeight);
PGraphics outputImage;
outputImage = createGraphics(imageWidth, imageHeight, P2D);
PImage testInputImage = new PImage();
testInputImage = loadImage("imputImage.png");
outputImage.beginDraw();
for (int i = 0; i < imageWidth; i++) {
for (int j = 0; j < imageHeight; j++) {
color thisPoint = testInputImage.get(i,j);
if ((blue(thisPoint) >= green(thisPoint)) && (blue(thisPoint) >= red(thisPoint))) {
stroke(clear);
} else {
stroke(testInputImage.get(i, j));
}
point(i,j);
outputImage.point(i,j);
}
}
outputImage.endDraw();
outputImage.save("outputImage.png");
}
1