How to save without background image

edited March 2015 in How To...

Hi everyone,

Sorry if I'm repeating a question that has already been answered, but I've scoured high and low and haven't found a good solution.

I'm saving images from my processing sketch, which contains a background image called in Setup. My code in Draw uses the background image's pixels to create designs using get(). However, when I save the image, I would like for the background of the final file to just be transparent.

Is there a way to have this happen? Options I've been exploring without effect include somehow getting back into Setup once the saving is initiated or hiding the background from view (like an option you see in Photoshop).

I'd be grateful for any thoughts or help,

Thanks,

David

Answers

  • edited March 2015
    • Rather than directly drawing to sketch's main canvas, we may choose to so in a PGraphics.
    • This way, we can use its method save() rather than canvas' save() or saveFrame()!
    1. https://processing.org/reference/PGraphics.html
    2. https://processing.org/reference/PImage_save_.html
  • Thanks for your help GoToLoop, but I think I've been doing this in my code, but haven't gotten results. Let me provide what I've been working with for some context:

    PGraphics big;
    PImage bg;
    PImage fragment;
    int counter = 0;
    color c;
    
    void setup() {
      bg = loadImage("black.jpg");
      big = createGraphics(2500, 2500, JAVA2D); //Create a new PGraphics
      big.beginDraw(); // Start drawing to the PGraphics object
      big.background(bg);
    }
    
     void draw() {
       big.pushMatrix();
    float a = random(457,501); //images are numbered 457-500.png
    int aint = int (a); //makes fragNumber an integer 
    
      // rotate + scale images
      int xint;
      int yint;
      float imageScale;
      float imageRotation = random(-360, 360);
      float x = random(big.width);
      float y = random(big.height);
      xint = int(x);
      yint = int(y);
      c = bg.get(xint, yint);
    
    
      if  (c == #000000) {
      big.translate(x, y);
      big.imageMode(CENTER);
      big.rotate(radians(imageRotation));
      big.scale(random(.04, .06)); //whiteflowers
      big.image(loadImage(aint+".png"), random(0, big.width), random(0,big.height));
      }
    
    big.popMatrix();
    
     }
    /** 
    * We save on any key 
    * this could be done in void close() but safer to have it here. 
    */  
    void keyPressed() {  
      big.endDraw(); // finish drawing  
      big.save("test" + ".png"); //save to file - use .tif as format for high-res  
      println("saved");
     println(frameCount); // nice with some feedback  
      println(hex(c));
    
    }  
    
  • edited March 2015 Answer ✓
    • My tip about PGraphics was to separate your background image from your foreground drawings!
    • So when you save() your PGraphics, your background image won't show up along!
    • However, you mix them up again w/ big.background(bg); @ line #11!
  • Thanks GoToLoop!

Sign In or Register to comment.