Create a transparent PNG from textbuffer


I searched everywhere for a solution to convert text to image and save it to disk in transparent PNG format.

Currently I am creating an ofBuffer where I load the text file and draw it on screen using drawString. Then I have an ofImage object where I grab the screen (grabScreen) and then saving the image.

void ofApp::update(){"timing_data.txt"), ofFile::ReadWrite, false); buffer = file.readToBuffer(); graafika.grabScreen(0,0,1024,768); graafika.saveImage("test.png", OF_IMAGE_QUALITY_BEST); }

void ofApp::draw(){
verdana14.drawString(buffer.getText() ,10,10);

The perfect solution would be to save the buffer directly into a transparent image, without drawing it out on the window.

PS. using OF version 0084 in code blocks for windows

Thanks in advance!

Hi, super quick one!

  1. Initialise an ofFbo with GL_RGBA
  2. Bind it, clear it, draw your text into it and unbind it.
  3. Read the ofFbo to an ofPixels which you save to a png.

exactly what hahakid said but just a note that when you clear, clear with ofClear(255,255,255,0) – ie, clear with full transparency, ie:


note that whatever color you clear with if you draw text that has alpha (most fonts have some alpha on the edges) it will blend with that color. So, if you clear the background with white and full alpha, and draw black text, you’ll see a white halo around the black text.

I don’t know if there’s a faster way, but I usually do this to read out the fbo and save it to an image:

ofPixels temp;
ofSaveImage("myImage.png", temp);

Hi. Thanks for the replies, got it working with the following code.

void ofApp::update(){"timing_data.txt"), ofFile::ReadWrite, false);
    buffer = file.readToBuffer();
    ofSetColor(0,0,0, 255);
    verdana14.drawString(buffer.getText() ,10,10);