Subscribe to DSC Newsletter

Producing Images from 2D Data Arrays or Text Files

Let's say you have a 2-dimensional array with 2,000 rows and 2,000 columns, with each cell value being a byte -- a value between 0 and 255, representing a palette color. Or you have a big text file, where each row represents a pixel and has the following fields:

  • x an y coordinate
  • red, green and blue color (each being a number between 0 and 255)

How do you turn these files into PNG, BMP or JPG images? Are there any software that can do that easily, and handle palettes, very easily? Or is your best option to use Python or Perl libraries (which ones?) to turn these raw text/binary files into standard image  formats? BMP format seems to be the easiest one to handle, but it's getting old. I've found this piece of code to solve this problem (see picture below, displaying an extract of the source code), but I could not make it to work properly. Can anyone help with this?

Views: 453

Reply to This

Replies to This Discussion

Here is some example code which generates an image from a 2D array of values, using Python:

Note that this uses Numpy to generate the fake data, and then it passes the numpy array into Matplotlib, a popular Python plotting library.  The choice of palette is just one keyword parameter, and there are number of nice ones to choose from.  Additionally, Matplotlib can save out in a wide variety of formats, and it automatically does the right thing based on the filename extension.  There's absolutely no need to muck around with manually trying to write out BMP file headers in Perl.


© 2021   TechTarget, Inc.   Powered by

Badges  |  Report an Issue  |  Privacy Policy  |  Terms of Service