Skip to content

Instantly share code, notes, and snippets.

@mvanga mvanga/highres.pde
Last active Feb 18, 2019

Embed
What would you like to do?
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");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.