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

In summary, Joe says that it is possible to input data into a fortran function that will create a picture. He provides a link to a website that explains how to do this.
  • #1
Joe1
11
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
  • #2
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 [Broken]
 
Last edited by a moderator:
  • #3


Hi Joe,
Yes, there are a few ways to convert a text file into an image file or generate an image using Fortran. One option is to use the Fortran library "ImageMagick" which allows you to create, edit, and convert images using various programming languages including Fortran. You can use the "convert" command within ImageMagick to convert your text file into an image file. Another option is to use the Fortran library "GD" which is specifically designed for creating images. You can input your data into the library and use it to create an image file. Additionally, you can use the "write" statement in Fortran to output your data into a specific image file format, such as PNG or JPEG. I hope this helps. Good luck with your project!
 

1. How can I convert a text file into an image using Fortran?

There are a few different ways to convert a text file into an image using Fortran. One method is to use the "write" statement to output the contents of the text file into an image file. Another method is to use a library, such as the GD library, which has Fortran bindings and can be used to generate images from text data.

2. Can Fortran be used to generate complex or dynamic images?

Yes, with the right libraries and coding techniques, Fortran can be used to generate complex and dynamic images. For example, the GD library mentioned above has features for creating images with multiple layers, different color spaces, and animation capabilities.

3. What are the benefits of using Fortran for image generation?

Fortran is a high-performance language that is well-suited for scientific and numerical computations. This makes it a good choice for generating images that involve complex mathematical calculations or large datasets. Additionally, Fortran has a long history in scientific computing and has a large community of users and resources available.

4. Are there any limitations to using Fortran for image generation?

While Fortran can be a powerful tool for generating images, it does have some limitations. It may not have as many built-in features for image manipulation as other languages or libraries. Additionally, Fortran is primarily a command-line language, so creating a graphical user interface for image generation may require additional tools or libraries.

5. Can Fortran be used to generate images for specific file formats?

Yes, Fortran can be used to generate images for specific file formats. This can be achieved by using libraries or modules that support the desired file format. For example, the NetCDF Fortran library can be used to generate images in the NetCDF file format commonly used in scientific and environmental data.

Similar threads

  • Computing and Technology
Replies
3
Views
942
Replies
6
Views
1K
  • Precalculus Mathematics Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
3
Views
647
  • Programming and Computer Science
Replies
16
Views
2K
Replies
4
Views
825
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Nuclear Engineering
Replies
4
Views
2K
  • Programming and Computer Science
Replies
12
Views
2K
Back
Top