# Code for saving the sketch as an image for printing

**URL:** <https://discourse.processing.org/t/code-for-saving-the-sketch-as-an-image-for-printing/2131>\
**Category:** Coding Questions\
**Created:** [July 27, 2018, 10:09am UTC](https://discourse.processing.org/t/code-for-saving-the-sketch-as-an-image-for-printing/2131 "2018-07-27T10:09:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![haschdl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/haschdl/32/1082_2.png) [@haschdl](https://discourse.processing.org/u/haschdl)\
**Post date:** [July 27, 2018, 10:09am UTC](https://discourse.processing.org/t/code-for-saving-the-sketch-as-an-image-for-printing/2131/1 "2018-07-27T10:09:26Z")

</div>

Anyone else creating art for print? 😃

I’m currently working on a project where I create images for printing, by drawing to an off-screen buffer (a PGraphics variable), and calling `save()` on the PGraphics. This allows me to create images larger than my screen resolution, which are likely necessary if you want a high-quality print .

After a few sketches and tweaks I put the code into a “reusable” PDE - in practice I copy and paste the same code to a separate tab when I start a new sketch. Due to my use of PGraphics object for the “final” image which I will print, I made the script to work in two ways:

1. “S” key will save the contents of the screen to JPG - good for checking out the output, or saving intermediates
2. “B” key will save the contents of the buffer to TIFF - it looks for a “buffer” variable (all of my sketches in the project). If “buffer” is not found, it tries to find a PGraphics variable regardless of name (a bit overkill, but prevents “breaking” a sketch if there is no “buffer” variable).

Copying and pasting is not best-practice in code reuse or software development, but Processing is not software development anyway 😃

Link to gist: [https://gist.github.com/haschdl/5efec3a87dde59460912567b7912e0ca](https://gist.github.com/haschdl/5efec3a87dde59460912567b7912e0ca)

Code:

```auto
/*
 * Function for saving the generated image into a file when you press "S" or "B" key. 
 * Files will be saved to /out folder, with name: [Sketch name]_[Timestamp].jpg
 * 
 * It works in two ways:
 * 1. S key (BASIC) 
 * If you press the S key ("Save"), it will save the contents of the screen. 
 * The image is saved in the same resolution as your sketch size. In other words, 
 * the image will have the same size as "height" by "width" pixels.
 *        
 * 2. B key (ADVANCED)
         If you press the B key (for "Buffer"), it will save the contents of an offscreen 
 * buffer (a PGraphics object). The image will have the size of the buffer, which
 * you defined during `buffer = createGraphics(bufferWidth,bufferHeight).` 
 *       
 * For B command, the code looks for a PGraphics variable called "buffer" (which
 * is the "standard" I use); if it cannot find "buffer", it looks for the first variable
 * of type PGraphics, regardless of name. This overengineered way is meant not to break
 * the sketch if there is no "buffer" variable - likely there are simpler ways to achieve 
 * the same.
 * 
 * Version: 27.07.2018
 */

import java.text.SimpleDateFormat;  
import java.util.Date;  
import java.util.*;
import java.lang.reflect.Field;

PGraphics bufferToSave;

void keyPressed() {
  String sketchName = this.getClass().getName();
  SimpleDateFormat formatter = new SimpleDateFormat("YYYYMMDD_HHmmss");  
  Date date = new Date();

  String fileName = String.format("/out/%s_%s.jpg", sketchName, formatter.format(date));
  if (key == 'S' || key == 's') {
    saveTo(null, fileName);
  } else if (key== 'B' || key == 'b') {
    bufferToSave = getBuffer();
    saveTo(bufferToSave, fileName);
  }
}

void saveTo(PGraphics source, String fileName) {
  if (source != null) {
    buffer.save(fileName);
    println(String.format("Contents of buffer saved to %s", fileName));
  } else {
    ((PApplet)this).save(fileName);
    println(String.format("Contents of screen saved to %s", fileName));
  }
}

/**
 *
 * Returns the first instance of PGraphics found in the sketch, in no particular order.
 *
 */
PGraphics getBuffer() {
  //looking for a variable called "buffer"
  try {
    Field bufferField = this.getClass().getField("buffers");
    if (bufferField != null)
      return (PGraphics)bufferField.get(this);
  }
  catch(NoSuchFieldException ex) {
  }

  catch ( IllegalAccessException ex ) {
    System.out.println(ex);
  }

  //if "buffer" not found, then look for first instance of PGraphics
  Field[] fields = this.getClass().getDeclaredFields();

  //print field names paired with their values
  for ( Field field : fields ) {      
    try {
      if (field.getType().getName().contains("PGraphics")) {        
        return (PGraphics)field.get(this);
      }
    }
    catch ( IllegalAccessException ex ) {
      System.out.println(ex);
    }
  }
  return null;
}

```

---

<div class="post-metadata">

**Author:** ![erwrow](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@erwrow](https://discourse.processing.org/u/erwrow)\
**Post date:** [July 27, 2018, 1:57pm UTC](https://discourse.processing.org/t/code-for-saving-the-sketch-as-an-image-for-printing/2131/2 "2018-07-27T13:57:38Z")

</div>

you could treat as pdf, see this:

[https://processing.org/reference/libraries/pdf/index.html](https://processing.org/reference/libraries/pdf/index.html)

or you could do the following:

```auto
PGraphics big; // Declare a PGraphics variable

void setup() {
  big = createGraphics(3000, 3000); // Create a new PGraphics object
  big.beginDraw(); // Start drawing to the PGraphics object 
  big.background(128); // Set the background
  big.line(20, 1800, 1800, 900); // Draw a line
  big.endDraw(); // Stop drawing to the PGraphics object 
  big.save("big.tif");
}

```

\*withdrawn from: [https://processing.org/tutorials/print/](https://processing.org/tutorials/print/)

---

<div class="post-metadata">

**Author:** ![haschdl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/haschdl/32/1082_2.png) [@haschdl](https://discourse.processing.org/u/haschdl)\
**Post date:** [August 1, 2018, 1:00pm UTC](https://discourse.processing.org/t/code-for-saving-the-sketch-as-an-image-for-printing/2131/3 "2018-08-01T13:00:13Z")

</div>

Thanks @erwrow! That’s pretty much what my code does, but “save” is called only you press “S” key in the keyboard.
