Need help making background transparent

I tried a lot of methods i saw, but none of them seem to work.
I am trying to take an image, save it and when I paste it into my canvas turn the white pixels into transparent.

here is some code that I have written so far(based on another forum post):
int q;
int c;
int x;
int y;
int i;
PImage save;
PImage newImg;
void setup() {
fullScreen();
background(255);

color c = get(25, 25);
fill©;
noStroke();
save = loadImage(“partialSave.png”);
}
void draw() {
fill(0);
rect(0, q, width, 50);
q = q + 50;
if ( c >= 255) {
c = 0;
} else {
c = c + 30;
}
fill(255);
rect(0, 0, 50, height);
rect(0, 0, width, 50);
rect(width-50, 0, 50, height);
rect(0, height-50, width, 50);
}
void mousePressed() {
save(“partialSave.png”);
}

void keyPressed() {
if (keyPressed && key == ‘s’) {
image(save, mouseX, mouseY, 100, 100);
loadPixels();
for (int i = 0; i < width*height; i++) {
if (pixels[i] == 0x000000) { // If it is red
pixels[i] = 0xff00ff00; // Make it green
}
}
updatePixels();
}
reset();
}

void reset() {
save = loadImage(“partialSave.png”);
}

I hope someone has some tips to help me on my way.

The sketch canvas cannot be transparent it is always opaque.
You can make a PGraphics or PImage layer that can be saved with transparency.
Further there is a weird flow in your code, first animating a rectangle, but then overdrawing it.

Hello,

Please format your code:
https://discourse.processing.org/faq#format-your-code

I did not look over your code; try to cut and paste it into a new sketch and you will understand why.

I was able to:

  frog1 = loadImage("frog1.png");
  frog2 = createImage(frog1.width, frog1.height, ARGB); //ARGB :)

Worked.

Additional resources:
Processing Images and Pixels Tutorial
10.5: Image Processing with Pixels - Processing Tutorial

:)

2 Likes