I'm working with a black & white camera and it gives me integer values to represent the amount of light hitting the sensor. Very bright is 1023 and totally dark is 0. The values are printed through serial and everything works fine but now I want to recreate the image based on the values.

Does anyone know if there is a simple way of doing this? I'm assuming the process would consist of looking up the value and giving a specific colored pixel in return, but I have no idea how to do this.

1 Answer 1


Simplest thing may be to write down your image in plain Portable Gray Map format. This is a very simple (and inefficient) ASCII format consisting of a signature (the string "P2"), followed by the width and height of the image, followed by the maximum possible gray level (1023 in your case), followed by the pixel values. It's a free form format, meaning you can mix spaces and newlines at your convenience.

Here is an example image: two rows of pixels, five columns, and brightness increasing left to right and top to bottom:

5 2
  0 111 222 333 444
555 666 777 888 999

Then, any image conversion software should be able to convert that into the format of your choice (PNG, JPEG, whatever...).

  • That kind of what I'm looking for, I just want to actually see the image and don't care much about efficiency. However, I did not get from the documentation how you would go about making of the these PGMs. Is it a file I make based on the numbers? Like do I take the values and past them into this PGM file?
    – sgmm
    Jul 5, 2016 at 18:49
  • @CrystalPritzker: It's just a text file. How would you write a text file on your computer? Jul 5, 2016 at 19:27
  • @CrystalPritzker, your sketch could write PGM-formatted data, which you pipe to a .pgm file on the host, or you could write a program that receives data organized however you like and writes it to a .pgm file. See eg tutorialspoint....c_file_io or cprogramming...cfileio re opening a file and writing to it. With a .pgm file, use a pgm-to-jpg (or whatever) program. (Search google for "pgm to jpg" and find eg pgm-to-jpg converters Jul 5, 2016 at 20:02
  • @EdgarBonet So are you saying that it's really as simple as writing the number in a text file and then ending it with .pgm? Then convert that file to an image?
    – sgmm
    Jul 6, 2016 at 0:09
  • @CrystalPritzker: In essence yes, except that 1) you have to write "P2" before the numbers, c.f. my example 2) You don't “convert that file to an image”: it's already an image, you just convert it to a more common image format. Jul 6, 2016 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.