Headless 2D drawing, saved as a jpeg

edited December 2016 in How To...

Hello,

I've been a programmer for a while and I am interested in using the built-in graphics libraries in Processing, I think can manage the Java part of Processing but I have two particular features that my programme would have and I'd like to check it would be possible in processing before I start :

  1. Can it work in a non-interactive mode (shewing no UI), in this case opening a file, doing some tasks and then closing?
  2. Can it draw to some non-visible area (of memory), e.g. using coloured lines, hatching, fills, text etc and then save that drawing as a jpeg?

Thanks for your help.

Tagged:

Answers

  • Answer ✓

    1) Sort of. You will always get a sketch window that appears, but there is no need to use it for anything. You can just leave it blank, do whatever you like, and the call exit() to close the sketch. If you really, really need a headless mode, I think there is one, but it's a trick and a half to get it to work.

    2) Yes.

    Here's an example sketch that may suit your needs:

    void setup(){
      size(200,200);
      background(0);
      fill(255);
      textAlign(CENTER);
      text("Unused window - Please ignore", 100,100);
    
      PGraphics pg = createGraphics(400,400);
      pg.beginDraw();
      pg.background(255);
      for( int i=0; i<100; i++){
        pg.fill(random(255),random(255),random(255));
        pg.ellipse(random(pg.width),random(pg.height),10,10);
      }
      pg.fill(0);
      pg.textAlign(CENTER);
      pg.text("Do you believe in magic?", 200,200);
      pg.endDraw();
      PImage pi = pg.get();
      pi.save("magic.PNG");
    }
    
    void draw(){
      exit();
    }
    
  • Thank you.

    The headless link goes to some technical Linux thing so I don't think it applies (I am trying to write this for a Windows desktop), but thanks for looking.

    If the programme runs quickly enough it might appear and dis-appear without being noticed or too obvious.

    Thanks for that, now all I need is to write the rest of the code (!).

  • edited January 2017

    @Quoque --

    On Windows specifically you should also be aware of the the command line option, if you aren't already:

    Can it draw to some non-visible area (of memory)

    The two built-in objects for non-visible pixel buffers in Processing are PImage and PGraphics. PGraphics (as used in TfGuy44 's example) also has drawing methods. Nothing is rendered to the screen unless you render the object with image().

Sign In or Register to comment.