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

AI Thread Summary
To convert a two-dimensional array of numbers into an image file, the discussion highlights the feasibility of creating either a RAW image format, such as BMP, or a compressed format like JPEG. For BMP files, which are simpler to create, the user needs to understand the bit depth options: 8-bit, 16-bit, or 24-bit, with 24-bit RGB being the most common. Each pixel's color is represented by three bytes corresponding to red, green, and blue values. The discussion emphasizes the importance of including header information to define image dimensions and suggests using binary output for pixel data. Resources for further reading on image formats and bitstream documentation are also mentioned, indicating that while JPEG creation is more complex due to compression algorithms like Discrete Cosine Transform (DCT), creating a BMP file is straightforward.
Joe1
Messages
11
Reaction score
0
Hello,
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.
Thanks,
Joe
 
Computer science news on Phys.org
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.

http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html#bmih
 
Last edited by a moderator:
Thread 'Urgent: Physically repair - or bypass - power button on Asus laptop'
Asus Vivobook S14 flip. The power button is wrecked. Unable to turn it on AT ALL. We can get into how and why it got wrecked later, but suffice to say a kitchen knife was involved: These buttons do want to NOT come off, not like other lappies, where they can snap in and out. And they sure don't go back on. So, in the absence of a longer-term solution that might involve a replacement, is there any way I can activate the power button, like with a paperclip or wire or something? It looks...
I came across a video regarding the use of AI/ML to work through complex datasets to determine complicated protein structures. It is a promising and beneficial use of AI/ML. AlphaFold - The Most Useful Thing AI Has Ever Done https://www.ebi.ac.uk/training/online/courses/alphafold/an-introductory-guide-to-its-strengths-and-limitations/what-is-alphafold/ https://en.wikipedia.org/wiki/AlphaFold https://deepmind.google/about/ Edit/update: The AlphaFold article in Nature John Jumper...
Back
Top