0

I am currently doing a project in Microsoft Small Basic to have some fun, though I have got stuck.

I have an array that I can export to a file in any format, which uses bytes as its smallest thing, such as a csv. Each pixel is a hex value, FFFFFF say, and it gets put into a file like:

FFFFFF,000000,FFF000,000FFF
000AAA,AAAAAA,AAA000,000000

etc...

Is there any way I could turn this into an image such as a bmp file or other raster format.

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Not much - I looked around online to see csv to bmp converters but there arent any really. – Oliver Bryant Feb 17 '18 at 08:33
  • So you have a single *specific task* and your research consisted of googling for a solution to exactly that *specific task*? Why not break it up into manageable parts, such as (1) read CSV, (2) create BMP? – Jongware Mar 04 '18 at 10:07

2 Answers2

2

Maybe you could write out your image in NetPBM's PPM format which is extremely simple and documented on Wikipedia here.

So, for example the following (enlarged) 3x2 image:

enter image description here

would look like this (the part following # on each line is my comment):

P3                               # P3 means ASCII, 3-channel RGB
3 2                              # width=3, height=2
255                              # MAX=255, therefore 8-bit
255 0 0 0 255 0 0 0 255          # top row of pixels in RGB order
0 255 255 255 0 255 255 255 0    # bottom row of pixels in RGB order

Then you can use ImageMagick, which is installed on most Linux distros and is available for macOS and Windows to make that into a BMP at the command line like this:

magick input.ppm output.bmp

or, if you want a JPEG with contrast stretch and resized to 800x600:

magick input.ppm -resize 800x600 -auto-level output.jpg

You can equally do the conversion with GIMP, Adobe Photoshop, probably MS Paint, probably IrfanView, or using the NetPBM toolkit. For example, with the NetPBM tools (which are much lighter weight than ImageMagick) the conversion would be:

ppmtobmp image.ppm > result.bmp
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
1

Building on Mark's fine answer, using ImageMagick and Unix scripting, one can do the following:

Convert your text file so as to replace commas with new lines, then add leading # to your hex values and store in an array (arr)

Then change each hex value into colors as rgb triplets in the range 0-255 integers with spaces between the 3 values and put into a new array (colors).

Find out how many rows and columns you have in your text file.

Then convert the array of colors into a PPM image and convert that image to bmp while enlarging to 300x200.


Here is the corresponding code:

arr=()
colors=()
arr=(`cat test.txt | tr "," "\n" | sed 's/^/#/'`)
num=${#arr[*]}
for ((i=0; i<num; i++)); do
colors[i]=`convert xc:"${arr[$i]}" -format "%[fx:round(255*u.r)] %[fx:round(255*u.g)] %[fx:round(255*u.b)]" info:`
done
numrows=`cat test.txt | wc -l`
numvalues=`cat test.txt | tr "," " " | wc -w`
numcols=`echo "scale=0; $values/$rows" | bc`
echo "P3 $numcols $numrows 255 ${colors[*]}" | convert - -scale 400x200 result.bmp

enter image description here

Note: I had to add a new line in the text file after your last color character, so that wc -l would compute the proper number of lines. That is the file must end with a new line character.

fmw42
  • 46,825
  • 10
  • 62
  • 80