ofImage Loading an Image from Pixels

I am trying to load an image from pixels stored in a pointer, can someone help me how to do it?

if (key==‘c’){

        unsigned char* pixels = vidGrabber.getPixels();

Hey @billy88, what you’re looking for is the ofImages’s setFromPixels function.


It’s arguments are a pointer to the array, the width and height and the image type (and therefore the number of channels)

Are you trying to copy pixels from the a video grabber?

unsigned char * pixels = vidGrabber.getPixels();

myImage.setFromPixels( pixels, vidGrabber.getWidth(), vidGrabber.getHeight(), OF_IMAGE_COLOR);

Thank you very much that is exactly what I was looking for, but I figured it out just a bit ago. Now that I got the image, I want to save a snapshot from the webcam every time i press a key button(basically a series of snapshots), but the it will not let me save except one image:

if (key==‘c’){

    //Getting pixels
   ofPixels pixels = vidGrabber.getPixelsRef();
   //Set pixel array to the image
   image.setFromPixels( pixels );
   for(int i,i<10,i++)

Sure, you’ll want to give each saved file a unique name then.

A common practice is to use a timestamp for the name. And there’s an OF function that does just that.

string timestamp = ofGetTimestampString();
string filename = "img_"+timestamp+".png";

FYI, in your code, you save the same image 10x. I’m not sure that’s what you were wanting to do.

Thank you this means a lot you’ve been very helpful. One more question, how do I load and draw them after I use a time stamp?

Never mind…I think I found a way