Create a transparent PNG from textbuffer

Hi.

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(){ file.open(ofToDataPath("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(){
ofBackground(0,0,0,0);
ofEnableAlphaBlending();
ofSetColor(255,255,255);
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:

fbo.begin();
ofClear(255,255,255,0);
fbo.end();

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;
fbo.readToPixels(temp);
ofSaveImage("myImage.png", temp);

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

void ofApp::update(){
    file.open(ofToDataPath("timing_data.txt"), ofFile::ReadWrite, false);
    buffer = file.readToBuffer();
    fbo.begin();
    ofClear(255,255,255,0);
    ofSetColor(0,0,0, 255);
    verdana14.drawString(buffer.getText() ,10,10);
    fbo.readToPixels(pix);
    ofSaveImage(pix,"test3.png");
    fbo.end();
}