Image to random circles conversion (PNG Problem) Java

Newbie here!
I have been working with some code from the internet and modified it to work from JS into Java. I have actually adapted everything so that the code works on its own.

My assumption was that when I use a PNG I only get dots on the non-transparent parts. Unfortunately, I get dots everywhere, even in places where the image is not. Transparent areas turn white and everything without an image turns black.

Does anyone have an idea how I can make it so that only the non-transparent part of the PNG gets the effect?

PImage shark;

void setup() {
  size(1000, 1000);
  stroke(255);
  noFill();
  shark = loadImage("shark_smal.png");
}

void draw() {
  //background(0);
  for (int j = 0; j<200; j++) {
    for (int i = 0; i < 100; i++) {
      int x = int(random(width));
      int y = int(random(height));
      int col = shark.get(x, y);
      noStroke();
      fill(red(col), green(col), blue(col), 190);
      float diameter = map(brightness(col), 0, 255, 5, 30);

      circle(x, y, diameter);
      println(i);
    }
  }
}

Take notice that the main canvas of a sketch is 100% opaque!

There are 2 workarounds for it I can think of:

Create a separate off-canvas via createGraphics() which can have transparent pixels:

Or check the alpha() component of a color and skips drawing it if it’s 0 (or less than some threshold):

2 Likes

Hi
Some hints

https://funprogramming.org/151-Convert-an-image-into-a-3D-grid-of-boxes.html

2 Likes