Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save a Processing sketch to a PNG file, server-side with no GUI/display

I'd like to use Processing to render a visualization on the server side (headlessly, with no GUI). The Processing sketch is static (i.e. does not animate), so I only need to grab the first frame, and I'd like to serve this result out to the users of our web application on-demand.

I've searched around a bit on the processing.org forums and it's been suggested that Processing is not intended to be launched headlessly. The only hack I've seen to do it is one involving launching a headless X11 display:

Xvfb :2 &
export DISPLAY=":2"
./myapp
killall -9 Xvfb

.. Which is not going to work for us as we'd like to have a pure-Java solution and can't always guarantee an X renderer on the server-side.

How do I do this in pure Java?

like image 235
Maciek Avatar asked Jun 22 '10 14:06

Maciek


People also ask

Where are processing sketches saved?

These files are saved to the sketch's folder, which may be opened by selecting "Show sketch folder" from the "Sketch" menu. Alternatively, the files can be saved to any location on the computer by using an absolute path (something that starts with / on Unix and Linux, or a drive letter on Windows).


2 Answers

Xvfb is likely to be faster than a java renderer, and a hardware-accelerated X server will be the fastest by a large margin, but if you want a 'pure' java solution you could try the Pure Java AWT Toolkit.

EDIT: Here's a boot command line example lifted from here:

java -Xbootclasspath:JDK/jre/lib/rt.jar:LIB/pja.jar -Dawt.toolkit=com.eteks.awt.PJAToolkit -Djava.awt.graphicsenv=com.eteks.java2d.PJAGraphicsEnvironment -Djava.awt.fonts=JDK/jre/lib/fonts mainclassname args
like image 90
Alexander Torstling Avatar answered Oct 05 '22 03:10

Alexander Torstling


Create a standard headless Java app, create a PGraphics object in it(1) and perform all of your drawing operations on that. Then save the PGraphics object to disk as an image file using .save().

1 You may need to obtain this from a PApplet, I'm not sure if you can create it directly.

The code will look mode or less like this:

PApplet applet = new PApplet();
PGraphics g = applet.createGraphics(200, 400, PApplet.JAVA2D) // same params as size()
g.beginDraw();
g.ellipse // ... etc, your drawing goes here
g.endDraw();
g.save("filename.png");
like image 26
Ollie Glass Avatar answered Oct 05 '22 03:10

Ollie Glass