Problem with PGraphics and images

edited April 2014 in Using Processing

Hello,

I'm encountering something really frustrating with PGraphics. I am trying to create something with that is 8000px by 8000px, so I'm using PGraphics to draw to an offscreen bufer. The problem is that whenever I use PGraphics, the code only draws one image and then stops. I have no idea why it doesn't continue to draw more images to the screen. I've seen examples where PGraphics can draw ellipses and other vector shapes over and over perfectly, but with images, PGraphics stops at one no matter the duration of the frames. Can someone please help me fix this???? Thank you. I'll post an example code I am using below:

PGraphics big;
PImage img;
void setup() {
  big = createGraphics(500, 500, JAVA2D); //Create a new PGraphics

 }

 void draw() {
big.beginDraw(); // Start drawing to the PGraphics object
img = loadImage("698.png");
big.image(img, random(0, big.width), random(0, big.height));
 }
/** 
* 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("highRes.jpg"); //save to file - use .tif as format for high-res  
  println("saved"); // nice with some feedback  
}  
Tagged:

Answers

  • General advise: Avoid loading files within draw(). Place those within setup()!
    Since you're placing a PImage at diff. coords. within the PGraphics, it's a good idea to clear() it before that!
    You should consider diff. names to save("") if you want multiple files!

  • 1) move img = loadImage("698.png") to setup() routine

    2)move big.endDraw() to draw() routine

    3)add image(big,0,0) as last line in draw() routine

  • I think he wants a patchwork effect. Multiple copies of the same source file, laid over each other.

    And I think the problem is the beginDraw() at the start of draw - it resets the current batch of drawings at the start of every frame, the same way background() would.

    Try moving lines 9 and 10 to setup()

  • Hi everyone, thanks for the responses.

    koogs, that's exactly what I'm going for - a patchwork, collage effect. I tried moving the lines of code up to setup() but nothing has worked. I still only see one image no matter how long I let the code run for. Urgh, I have no idea why it's doing this. If I do shapes instead of images, the code works just fine. Something about the images is being screwy.

  • Here's working code that incorporates some of the above suggestions. It shows you a quarter-size view of the whole big PGraphics, but saves it large. If you take "big" all the way to 8000x8000 pixels, you'll need to increase available memory in Preferences.

    PGraphics big;
    PImage img;
    int counter = 0;
    
    void setup() {
      size(500,500);
      big = createGraphics(1000, 1000, JAVA2D); //Create a new PGraphics
      img = loadImage("698.png");
     }
    
    void draw() {
      big.beginDraw(); // Start drawing to the PGraphics object
      big.imageMode(CENTER);
      big.image(img, random(big.width), random(big.height));
      big.endDraw(); // finish drawing 
      image(big,0,0,500,500); 
     }
    
    void keyPressed() {  
      big.save("highRes" + nf(counter, 3) + ".png");
      println("saved " + str(counter)); // nice with some feedback  
      counter++; 
    }  
    
  • Thank you all for your help! I solved the problem using PhiLho's suggested popMatrix. :((

Sign In or Register to comment.