Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving numerical 2D array to image

Lately I have been doing some numerical method programming in C. For the bug fixing and throubleshooting it's nice to have some visual representation of what is happening. So far I have been outputting areas of array to the standard output, but that doesn't give that much information. I have also been playing around a bit with gnuplot, but I can't get it too save only image, not the coordinate system and all the other stuff.

So I am looking for a tutorial or maybe a library to show me how to save array from c into an image, it would be especially nice to be possible to save to color images. The transformation from numerical value to a color is not a problem, I can calculate that. It would just be nice of someone to point me in the direction of some useful libraries in this field.

best regards

like image 910
Rok Avatar asked Dec 03 '10 15:12

Rok


People also ask

How do I save an array image?

Create a sample Numpy array and convert the array to PIL format using fromarray() function of PIL. This will open a new terminal window where the image will be displayed. To save the Numpy array as a local image, use the save() function and pass the image filename with the directory where to save it.

Is an image a 2D array?

Each pixel is characterised by its (x, y) coordinates and its value. Digital images are characterised by matrix size, pixel depth and resolution. The matrix size is determined from the number of the columns (m) and the number of rows (n) of the image matrix (m × n).


1 Answers

You could use the .ppm file format... it's so simple that no library is necessary...

FILE *f = fopen("out.ppm", "wb");
fprintf(f, "P6\n%i %i 255\n", width, height);
for (int y=0; y<height; y++) {
    for (int x=0; x<width; x++) {
        fputc(red_value, f);   // 0 .. 255
        fputc(green_value, f); // 0 .. 255
        fputc(blue_value, f);  // 0 .. 255
    }
}
fclose(f);
like image 107
6502 Avatar answered Sep 20 '22 23:09

6502