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
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.
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).
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With