Skip to content

Instantly share code, notes, and snippets.

@hdeshev
Created March 3, 2012 10:10
Show Gist options
  • Save hdeshev/1965350 to your computer and use it in GitHub Desktop.
Save hdeshev/1965350 to your computer and use it in GitHub Desktop.
Render a Processing (http://processing.org) scene to an image
//Invoke with: scala -cp /opt/processing-1.5.1/lib/core.jar processing.scala
import processing.core._
import java.io._
import java.awt.image._
import javax.imageio._
class Image extends PApplet {
private[this] val width = 200
private[this] val height = 200
//Don't override those or you get stuck in the Animation thread endless loop when calling init
//override def setup {
//size(width, height)
//background(0)
//}
//override def draw {
//stroke(255)
//line(10, 10, 50, 50)
//}
def renderToImage = {
init
size(256, 256)
background(0)
stroke(255)
line(10, 10, 50, 50)
line(50, 10, 10, 50)
noLoop
loadPixels
val img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
img.setRGB(0, 0, width, height, pixels, 0, 256)
draw
img
}
}
object Program {
def main(args: Array[String]) {
val image = new Image
val os: OutputStream = new FileOutputStream("out.png")
ImageIO.write(image.renderToImage, "PNG", os);
os.close
}
}
Program.main(Array())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment