Skip to content

Instantly share code, notes, and snippets.

@mvanga
Last active April 6, 2023 22:32
Show Gist options
  • Save mvanga/b1b8839f94aae70e34cfa3fb334e8845 to your computer and use it in GitHub Desktop.
Save mvanga/b1b8839f94aae70e34cfa3fb334e8845 to your computer and use it in GitHub Desktop.
A skeleton Processing sketch that allows for exporting high-resolution versions of whatever is on screen.
import processing.pdf.*;
/*
* A complete tutorial can be found over at:
* http://sighack.com/post/exporting-high-resolution-images-in-processing
*/
int seed;
int CONFIG_SCALE_FACTOR = 5;
void setup() {
size(720, 720);
seed = millis();
seededRender();
}
void draw() {
}
void seededRender() {
randomSeed(seed);
noiseSeed(seed);
render();
}
void render() {
/* Write your drawing code here */
smooth();
noStroke();
colorMode(HSB, 360, 100, 100);
background(255, 0, 100);
float hue = random(360);
for (int i = 0; i < 10000; i++) {
float r = random(10, 50);
fill(hue, 50 + random(-50, 50), 50 + random(-50, 50), 50);
ellipse(random(-width/2, width * 5/4), random(-height/2, height * 5/4), r, r);
}
}
/*
* The keyPressed handler handles the following
* -- If the key 's' (lower case) is pressed, then
* it will save a low-resolution version of the
* image into a file 'lowres-<seed>.png'.
*
* -- If the key 'h' (lower case) is pressed, then
* it will save a high-resolution version into
* the file 'highres-<seed>.png', scaled by an
* amount set in the SCALE_FACTOR variable.
* So if your screen width is 500 pixels, a
* scale factor of 10 will generate a
* high-resolution version that is 5000x5000px.
*
* -- If the key 'p' (lower case) is pressed, then
* it will save a vector version as PDF into the
* file 'vector-<seed>.pdf', which is inherently
* scalable to any resolution.
*
* -- If any other key is pressed, a new random
* seed is generated and the render function
* called again to generate a new artwork.
*/
void keyPressed() {
if (key == 's') {
saveLowRes();
} else if (key == 'h') {
saveHighRes(CONFIG_SCALE_FACTOR);
} else if (key == 'p') {
savePDF();
} else {
seed = millis();
seededRender();
}
}
void saveLowRes() {
println("Saving low-resolution image...");
save(seed + "-lowres.png");
println("Finished");
}
void saveHighRes(int scaleFactor) {
PGraphics hires = createGraphics(
width * scaleFactor,
height * scaleFactor,
JAVA2D);
println("Saving high-resolution image...");
beginRecord(hires);
hires.scale(scaleFactor);
seededRender();
endRecord();
hires.save(seed + "-highres.png");
println("Finished");
}
void savePDF() {
println("Saving PDF image...");
beginRecord(PDF, seed + "-vector.pdf");
seededRender();
endRecord();
println("Finished");
}
@order-of
Copy link

order-of commented Jul 6, 2020

hey is there a way to use the saveHighRes function with p3d? changing renderer to p3d gives this error:

Framebuffer error (incomplete missing attachment), rendering will probably not work as expected Read http://wiki.processing.org/w/OpenGL_Issues for help.
OpenGL error 1286 at bot beginDraw(): invalid framebuffer operation
OpenGL error 1286 at top endDraw(): invalid framebuffer operation
OpenGL error 1286 at bot endDraw(): invalid framebuffer operation

@mvanga
Copy link
Author

mvanga commented Jul 6, 2020

Hey @order-of, I haven't tried this with p3d as I focus almost entirely on 2D output. It appears that the beginDraw/endDraw functions have issues with it, although I haven't been able to find any mention of this w.r.t. Processing. If you do dig into it and find a fix, please do consider pushing a patch.

@Yiorj
Copy link

Yiorj commented Dec 28, 2021

Thanks, It hleps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment