how to make a pixel transparent?

edited March 2017 in How To...

I seem to recall there was already a topic on this but I can't find it ... so what I want to do is loop through an array of pixels and make all the pixels above a certain brightness value transparent. How would I do this? Thanks!

Answers

  • edited March 2017 Answer ✓
    // forum.Processing.org/two/discussion/21653/how-to-make-a-pixel-transparent
    // GoToLoop (2017-Mar-28)
    
    PImage makeTooBrightColorTransparent(final PImage img, final int bright) {
      img.loadPixels();
      final color colors[] = img.pixels, len = colors.length;
    
      for (int i = 0; i < len; ++i) {
        final color c = colors[i];
        if (brightness(c) >= bright)  colors[i] = c & ~PImage.ALPHA_MASK;
      }
    
      img.updatePixels();
      return img;
    }
    
  • Thanks! It seems to work, but not perfectly, on a video too ... most of the time it works but at times some of the bright pixels are not being turned transparent ... weird ...

  • edited March 2017

    You haven't said anything about video! [-(
    If method read() is called under another Thread, its pixels[] is completely filled by the new arrived frame! :-@

  • Ah ok ... sorry :/

    Uh, so my code look like this right now:

    void draw() {      
    if (movie.available()) {   
            movie.read();
            image(makeTooBrightColorTransparent(movie, 100), 0, 0);
          } 
    }
    
  • Seems correct. Method read() is called under the "Animation" Thread.
    So it should just work. Dunno why it fails sometimes for ya according to what you've posted so far.

Sign In or Register to comment.