Ensure no transparency before drawing PImage

edited April 2017 in How To...

i have a PImage that i load on an intiation and that i need to draw at the end of my draw loop so it could be above all other drawings and without transparency, despite the previous code how do i ensure this?

i mean 'init' 'arbitrary code' loadImage 'arbitrary code' '/init'

'draw' 'arbitrary code'image(img,x,y)'/draw'

I tried all of:

            <init>

....

    father.tint(255,255,255);
    father.fill(255,255,255);
    father.alpha(father.color(255, 255));
    img=father.loadImage("name\\"+name+".jpg");

....

< /init>

            <draw>

....

    father.tint(255,255,255);
    father.fill(255,255,255);
    father.alpha(father.color(255, 255));
    father.image(img, posX+xd-img.width,  posY+yd);

< /draw>

but i can still se lines behind the image.

Answers

  • The PImage has some pixels transparent ?
    You can check every pixel with pixels[i] == 0x0 and then replace it with your color.
    cameyo

  • edited April 2017 Answer ✓

    @JDev -- re:

    and without transparency, despite the previous code

    @cameyo's method of manually stripping transparency from img is one approach that would work. Here are some other things you can do.

    1. your father is a PGraphics, your img is a PImage -- please don't post incomplete code without data types, it makes your descriptions potentially misleading. Instead, post a minimal sketch that demonstrates the problem

    2. In general, you should use pushStyle() and popStyle() to isolate the style changes in your previous code so that you aren't unsure of what they are later and unclear on how to undo them. https://processing.org/reference/pushStyle_.html

    3. PGraphics.alpha() doesn't set anything -- it "Extracts the alpha value from a color" (like .red()) You are then throwing that value away. http://processing.github.io/processing-javadocs/core/processing/core/PGraphics.html#alpha-int-

    4. PGraphics.fill() sets the color used for drawing -- it doesn't actually change pixels the way that PGraphics.background() does. If you want to set the background of father, call father.background(255) https://processing.org/reference/PGraphics.html.

    If you don't want to cover all of father in white, but you do want only the area behind your transparent image to be white, then either draw a white rect behind your image:

    father.fill(255);
    father.rect(posX+xd-img.width,  posY+yd, posX+xd,  posY+yd+image.height)
    father.image(img, posX+xd-img.width,  posY+yd);
    

    ...or, more simply, change your father.blendMode() before drawing the transparent image in order to alter the way it handles merging transparency onto the PGraphics canvas. https://processing.org/reference/blendMode_.html

    father.blendMode(REPLACE);
    father.image(img, posX+xd-img.width,  posY+yd);
    
  • edited April 2017

    father.blendMode(REPLACE); did work, how do i set it back to what it was before i tried pushStyle(); father.blendMode(REPLACE);image; popStyle(); but something is now different in other blendings. father.blendMode(BLEND) didnt work either

  • edited April 2017

    To push and pop style on a PGraphics, use the PGraphics.pushStyle(): http://processing.github.io/processing-javadocs/core/processing/core/PGraphics.html#pushStyle--

    father.pushStyle();
    father.blendMode(REPLACE);
    father.popStyle();
    

    Or, set blendMode back to the default manually. As per the documentation:

    BLEND - linear interpolation of colours: C = Afactor + B. *This is the default blending mode.

    father.blendMode(BLEND);
    
  • yep, sorry if i havent expressed myself clearly but thats what i wrote(or wantend to write) im now using the akward transform(0,0,1) and (0,0,-1) later with some degree of success, it works but i get a little offset.

Sign In or Register to comment.