Saving image from PGraphicsPDF

Hmm. I'm trying to save a PGraphics object as a PNG file, which I am rendering as a PDF. But when I try the usual pg.save("output.png") it throws an error: "No save() for PGraphicsPDF".

Another try, but also doesn't work: PImage img = pg.get() throws the error "No get() for PGraphicsPDF". A search of the PGraphics listing in the Javadoc didn't turn anything up.

Help?

Answers

  • Whoa, forum auto-posting freakout - sorry for the double post!

  • edited July 2014

    PDF records what's being rendered on-the-fly via beginRecord() & endRecord().
    Then saves it to a pre-defined file. Below's a simple example:

    /**
     * Letters-X (v3.01)
     * by  DiegoOriani (2014/Apr)
     * mod Chrisir & GoToLoop
     *
     * forum.processing.org/two/discussion/4275/
     * how-to-compile-a-series-of-functions-into-one-variable
     */
    
    import processing.pdf.PGraphicsPDF;
    
    interface DataX {
      int ROWS = 8, COLS = 10, GAP = 030, MARGIN = 020;
      int LEN = 030, WEIGHT = 5, CAP = SQUARE;
    
      int HSIZE = COLS*(LEN+GAP) + MARGIN*2 - GAP;
      int VSIZE = ROWS*(LEN+GAP) + MARGIN*2 - GAP;
    
      color BG = 0300;
    
      color[] inks = {
        //#FFCC00, #FF9900, #FF35EE
        #FF0000, #008000, #0000FF
      };
    
      String PDF_FILE = "lettersX-####.pdf";
    }
    
    final LetterX[] xxx = new LetterX[DataX.ROWS * DataX.COLS];
    boolean savePDF;
    
    void setup() {
      size(DataX.HSIZE, DataX.VSIZE, JAVA2D);
      noLoop();
      frameRate(10);
    
      initCanvas();
      instantiateLettersX();
    
      println("Width: " + width + "\tHeight: " + height);
      println("# of Xs: " + xxx.length + "\n");
    }
    
    void initCanvas() {
      smooth(4);
      background(DataX.BG);
      strokeWeight(DataX.WEIGHT);
      strokeCap(DataX.CAP);
    }
    
    void draw() {
      if (savePDF) {
        beginRecord(PDF, dataPath(DataX.PDF_FILE));
        initCanvas();
      }
    
      for (LetterX currentX: xxx)  currentX.display();
    
      if (savePDF) {
        endRecord();
        savePDF = false;
        println("Frame #" + frameCount + " saved!");
      }
    }
    
    void keyPressed() {
      reshuffleInks();
      redraw();
    
      if (keyCode == 'S')  savePDF = true;
    }
    
    void mousePressed() {
      keyPressed();
    }
    
    void reshuffleInks() {
      for (LetterX currentX: xxx)
        currentX.c = DataX.inks[(int) random(DataX.inks.length)];
    }
    
    void instantiateLettersX() {
      for (int r = 0; r != DataX.ROWS; ++r)
        for (int c = 0; c != DataX.COLS; ++c) {
          int x = c*(DataX.LEN + DataX.GAP) + DataX.MARGIN;
          int y = r*(DataX.LEN + DataX.GAP) + DataX.MARGIN;
          color ink = DataX.inks[(int) random(DataX.inks.length)];
    
          xxx[r*DataX.COLS + c] = new LetterX(x, y, ink);
        }
    }
    
    class LetterX implements DataX {
      final short x, y;
      color c;
    
      LetterX(int xx, int yy, color cc) {
        x = (short) xx;
        y = (short) yy;
        c = cc;
      }
    
      void display() {
        stroke(c);
        line(x, y, x+LEN, y+LEN);
        line(x+LEN, y, x, y+LEN);
      }
    }
    
  • edited July 2014

    GoToLoop, I fear that again you throw in hastily some code not directly related to the question.

    Here is one way to draw in two different PGraphics:

    import processing.pdf.*;
    
    PFont f;
    int prevX;
    PGraphics pdf, other;
    
    void setup()
    {
      size(400, 400);
      f = createFont("Technical", 72);
      prevX = width/2;
      noLoop();
      pdf = createGraphics(400, 400, PDF, "H:/Temp/Test.pdf");
      pdf.beginDraw();
      other = createGraphics(400, 400, JAVA2D);
      other.beginDraw();
    }
    
    void draw()
    {
      println("Making page " + frameCount);
      drawOn(pdf);
      drawOn(other);
    
      // When finished drawing, quit and save the file
      println("Done");
      pdf.dispose();
      pdf.endDraw();
      other.endDraw();
      other.save("H:/Temp/Test.png");
      exit();
    }
    
    void drawOn(PGraphics pg)
    {
      pg.background(255);
      pg.fill(#005500);
      pg.stroke(#000055);
      pg.strokeWeight(5);
      pg.textFont(f, 72);
      int newX = int(random(0, width));
      // Draw something good here
      pg.line(prevX, 0, newX, height);
      prevX = newX;
      pg.text("Page " + frameCount, 100, 200);
    }
    
Sign In or Register to comment.