LazerDirector

So here is my simple little processing sketch that made all the nice circles.


Here is the source and example in a nice zipfile: LazerDirector

And here is the source, copy pasted, nice!

/* Lazor Director is a processing.org sketch to make halftone images into svg files that can be sent to a laser-cutter service,
examples of these are

http://www.cutyourway.com/
http://ponoko.com/
and
http://www.1scale1.com/

Please note that the processing image is not a real representation of how the svg looks, the svg has far better resolution

*/

PrintWriter output; // We use this to pritnt the svg file

PImage photo; //the original image that we want to convert to halftoneish svg

void setup()
{
  smooth(); //we want the smoothest possible edges in the image we see, can be removed for speed

  photo = loadImage("lobo.png"); //input image, that is, here is where you load what you want to halftone-hole (make sure it has the same size as the svg file output(se further down)) 
  size((int)(photo.width), (int)(photo.height));  //change processings window output widht and height

  output = createWriter("output.svg"); 

  background(0);  //setting the background to black, this is because I have black pastic to cut out from, add here your materials color

/*this is the header of the svg file. , 1080*1080 is 12"*12" (according to inkscape) */
  output.println("");
  output.println(""); //1080 * 1080 is 12"x12"

  output.println(" Test");
  output.println("");
  output.println(" Layer 1");

  drawCircles2(photo, 12); //the function that does the finding of the circles, second parameter is how large chunks (in pixels)it shall search in.
  output.println("");
  output.println("");
  save("output.png");
  output.flush();
  output.close();  

}  

void draw()
{
//nothing to do here
}  

void drawCircles2(PImage photo, int spacing)
{
  for(int j = 0; j < photo.height; j+=spacing) //looping through the image, in squares of 
  {
    for(int i = 0; i < photo.width; i+=spacing)
    {
      PImage segment = photo.get(i, j, spacing, spacing); //cuting out a whole
      float brighnezz = getAreaBrightness(segment); //meassuring the brighness
      float cSize =( brighnezz/51.2); //adjusting the size of the whole depending on the brightnezz

      if(cSize >1.5) //to small holes, we dont care about (was 0.5 in the one I show over at:
      {
        fill(255); //white here because I want to get a feel for how it would look if added light behind
        stroke(255); //same here
        /*writing the svg information to file */
        output.println(" +cSize+"\" cy=\""+j+"\" cx=\""+i+"\"/> ");
        /* drawing preview on screen */
        ellipse(i, j,cSize, cSize);
      }
    }
  }
}   

int getAreaBrightness(PImage area)
{
  int bval = 0, count = 1;

  for(int i = 0; i < area.width; i++)
  {
    for(int j = 0; j < area.height; j++)
    {
      color check = area.get(i, j); 

      bval += brightness(check); //thank you processign for having all the dedious task's as functions!
      count++;
    }
  }
  return (int)(bval/count);
}

Leave a Reply