Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Convert a text file into an image (or generate an image with fortran)

  1. Dec 16, 2005 #1
    I just made a program that puts numbers into a two dimensional array, and then saves them in a text file that looks something like this:
    4 4 4 4 4 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6
    4 4 4 4 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 7
    4 4 4 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 7 7
    4 4 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 7 7 7
    (except it is 800 by 800)
    is there any way I can make the text file into a picture file and have the numbers represent colors of individual pixels? Otherwise is there a way to input data into some fortran function that will give me an image? Any type of image file is good.
  2. jcsd
  3. Dec 16, 2005 #2


    User Avatar
    Homework Helper

    Yes. In fact, you will find that it can be relatively easy. You have to be aware first that there are various formats for images. You may have heard of CompuServ's GIF, or JPEG, or TIFF. These formats are non-RAW, meaning they are encoded (e.g. compressed) in a certain way. JPEG uses the Discrete Cosine Transform (DCT) for example, and if you want to create a JPEG file, you can refer to bitstream documentation available on the web which will include overhead information (which can unfortunately become complex). Algorithms for DCT are also widely available.

    However, if you are only wishing to create a RAW picture (which is the easy case), you will be looking at BMP or YUV (for the RAW video case). Again, bitstream documentation is available on the web, but the basics go as follow:

    For BMP, there is an option of whether you want 8-bit, 16-bit, 24-bit.

    8-bit means you have a total of 256 color choices per pixel on the screen, 16-bit is 2^16 and so on. Normally, 24-bit (RGB) is used. So if you wanted to show a white pixel in 24-bit RGB, you would output:

    FF FF FF

    The first byte (FF) is for max red, the next for max green, the next for max blue. In pascal or C, use char and putchar() for each byte. Make sure you are using binary output as well. You may need a look up table if you want to find out what color (45 FE 22) is for example.

    As usual, header information is required, so that your image software knows how big the image dimensions are. The bitstream is literally a stream. Try opening a BMP file using a hex editor. You will see that the pixel values just go on and on. You can read up on this at the link i posted below.

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Convert a text file into an image (or generate an image with fortran)
  1. Image file processing (Replies: 18)

  2. Save files as images (Replies: 8)