Error Exporting Image

edited September 2015 in Programming Questions

When I try to save my sketch at the end of it, my sketch (which runs normally) turns to a grey screen and the .jpg that is created is empty. It used to save a partially-drawn sketch, now it is saving a completely unopenable file.

code:

// source: http://natureofcode.com/book/chapter-8-fractals/
void setup(){
 size(840, 360); 
 noFill();
 smooth(8);
 strokeWeight(0.25);
 background(255); 
}

void draw(){
  drawCircle(width/2, height/2, 400);
}

void drawCircle(float x, float y, float radius) {
  float mult = 1;
  ellipse(x, y, radius, radius);
  if(radius > 9) {
    drawCircle(x + mult*radius/2, y, radius/2);
    drawCircle(x - mult*radius/2, y, radius/2);
    drawCircle(x, y + mult*radius/2, radius/2);
    drawCircle(x, y - mult*radius/2, radius/2);
  }
if ( keyPressed ) {
  saveFrame("test.pdf");
}
}

I also tried putting an else statement instead of

if (keyPressed) 

to save the sketch once radius gets below 8, but the same issue exists.

Screenshot: Screen Shot 2015-09-24 at 10.16.04 PM

If you can tell, here are two of the unopenable files, showing the partially drawn sketches: Screen Shot 2015-09-24 at 10.47.13 PM

Tagged:

Answers

  • Try using keyboard event method instead of variable mousePressed:

    void keyPressed(){
    saveFrame("test.pdf");
    }
    
  • edited September 2015 Answer ✓

    @kylienic==== try this

        void setup(){
         size(840, 800); 
         noFill();
         smooth();
         strokeWeight(0.25);
         background(255); 
        }
    
        void draw(){
          drawCircle(width/2, height/2, 400);
          if ( keyPressed == true ) {
    
          saveFrame("test.png");//instead of.pdf and in draw though it resolves in .tiff
        }
        }
    
        void drawCircle(float x, float y, float radius) {
          float mult = 1;
          ellipse(x, y, radius, radius);
          if(radius > 9) {
            drawCircle(x + mult*radius/2, y, radius/2);
            drawCircle(x - mult*radius/2, y, radius/2);
            drawCircle(x, y + mult*radius/2, radius/2);
            drawCircle(x, y - mult*radius/2, radius/2);
          }
        //if ( keyPressed == true ) {
        //   drawCircle(width/2, height/2, 400);
        //  saveFrame("test.png");
        //}
        }
    
  • use the keyPressed() method (or keyReleased()) rather than the keyPressed flag - the keyPressed flag will probably be true for multiple frames so there will be overwriting of the image.

    and saveFrame() doesn't work with pdf. the page explaining pdf export schemes is quite thorough. https://processing.org/reference/libraries/pdf/index.html

  • edited September 2015

    @koogs:: the code i have posted for answering works: i tested && it generates only one image if you add .pdf (instead of .png or...) it works also, but it adds .tiff after (tiff is default), so you get a file test.pdf.tiff

  • but i'm guessing he wants a pdf, a scalable image, and not just pixels.

    it only generates one image because you've only got one image name in there and the later images are overwriting the first. if you used the ## in the filename i can pretty much guarantee that no matter how careful you are you will get more than one image saved.

    but, yes, yours is an improvement because saveframe is called from draw() whereas the original could be called from anywhere within the heap of recursive calls to drawCircle.

  • @koogs::: he asked for saveFrame() not for pdf; yet i commented that .pdf was strange in his code... As for keyPressed vs keyPressed() i think that the keyPressed event is fired also and firstly by keyPressed(); but the best is to test yourself: tell me what happens coding with saveFrame(test-####.png)...

  • he asked for saveFrame() not for pdf

    the way i figure it, someone new to processing is more likely to know the file type she wants than whether the a method is capable of saving that filetype.

  • as for the keyPressed stuff, i tried this:

    void setup() {
    }
    
    void draw() {
      background(128);
      if (keyPressed) {
        saveFrame("flag_###.jpg");
      }
    }
    
    void keyPressed() {
      saveFrame("pressed_###.jpg");
    }
    
    void keyReleased() {
      saveFrame("released_###.jpg");
    }
    

    ran it and hit a key

    it created 5 files:

    pressed_430.jpg - from keyPressed method flag_0431.jpg flag_0432.jpg flag_0433.jpg - all from within draw released_433.jpg - from keyReleased method

    which is exactly as i'd seen before.

  • @koogs:: it seems to me that the keyevent keypressed (boolean) is fired at each loop, so 1 or ++ images depend a) of frameRate ---(what do you do in your draw???--b) of the time you stay on the key: if you stay on it you get as many images you want, 1 at each loop; if you hit quickly, only 1; this is exactly the same for keyPressed(); try your code without keyPressed boolean && keyReleased() and stay on the key... So, as for me, the "true" solution (for most cases) is keyReleased() which is fired only once.

Sign In or Register to comment.