How to Draw Off-screen and Save to a File

#1

Hi folks,

My question is I would like to be able to have the various drawing commands, like ofEllipse for example, draw to a memory buffer and not the screen and then save that drawing as a file. For example, here is the code in Processing to accomplish that very task.

  
  
PGraphics png;   // buffer to hold the picture  
  
void setup() {  
 png = createGraphics(4000,4000, JAVA2D); //create buffer bigger than screen  
 png.beginDraw();  // okay  - go  
 png.background(0,0,0);  // set background to black       
 png.Fill(255,0,0);  
 png.ellipse(100,100,2000,2000);   // draw an ellipse  
 png.ellipse(3100,3100,900,900); // draw an ellipse  
 png.endDraw(); // okay - done  
 png.save("MyOutputPicture.PNG"); // save the buffer to a PNG file  
 exit();  
} // end setup  
  

Can someone advise me as to what would be the equivalent in openframeworks?

Thanks, Jim

1 Like
#2

Looking for answer to the same problem.
I’m using Mac OS X and I always have a black bar in the middle of the grabbed image.

1 Like
#3

Make an fbo, draw to that, then load it into an image. something like

ofFbo fbo;
ofImage img;

fbo.allocate(100, 100, GL_RGBA);

fbo.begin();
ofClear(0, 255);
ofSetColor(255);
ofCircle(…);
fbo.end();

fbo.readToPixels(img.getTextureReference());
img.save(“foo.png”);

something like that anyway.

2 Likes
#4

Hi, pants.

I met this error when run fbo.readToPixels(img.getTextureReference());

“No matching member function for call to ‘readToPixels’”.

ScrShot%2012

Thanks.

from @bemoregt.

What’s wrong to me?

#5

I’ve always been saving fbos to disk using the following code

 ofPixels pix;
 fbo.readToPixels(pix);
 ofSaveImage(pix, fileName + ofToString(ofGetFrameNum()) + ".png", OF_IMAGE_QUALITY_BEST);

Where fileName is a string variable with the name of the file I’d want it to be, declared earlier. Also, this is usually in my update and saves as an image sequence, which is why ofGetFrameNum() is used so as to not overwrite the saved file on every update cycle. Can be omitted if you’re only saving once.

1 Like
#6

if helpful this is some code I use for generating high res / large images from OF:

the code is oriented towards 8.5x11 aspect ratio but you can adjust those numbers in setup. it gives you the largest possible offscreen fbo. when you hit space it saves a png.

this is the code I use for my own printing (https://zachlieberman.myshopify.com) and it’s pretty easy – I get about 7000x9000 pixels on my graphics card

for larger stuff you you can look at tile renderers, etc…

4 Likes
#7

Thank you for this code Zach. I wanted to know (if you want to share with us) which print machine are you using for your art ?

#8

epson p800 on hot press bright

1 Like