unix commands to convert text pixel descriptions to and from common image file types

Viewed 31

Back a few decades, I recall on a Solaris system stumbling across a family of commands that convert an image file (jpeg, bmp, etc.) to and from a textual form that described images as pixel arrays and pixels on one line of text each with the red, green, blue as ascii digits. With this, you could easily create, process, and modify images in any language without getting bogged down in the image file specifications.

Can someone give me a lead?

2 Answers

You may want to have a look at the PPM format from the Netpbm family.

As for the command line tools, apart from the ones coming with the code, ImageMagick handles the Netpbm formats very well. Just use convert command.

Netpbm formats don't do any compression and the converters, for a given pixel data, produce an uniform file. I successfully use this feature in test suites, making sure that the image generated by the software has the desired content - see the example (it is Go, but could be anything with Netbpm bindings).

As @WojciechKaczmarek says, use NetPBM. If you want a concrete example with ImageMagick you need to use -compress none for the ASCII versions (i.e. P1, P2 and P3) of NetPBM files.

So, make a 3-pixel wide image with one black, one white and one lime green pixel:

magick xc:black xc:white xc:lime +append image.png

Upscaled Output - so you can see it:

enter image description here

Now convert to ASCII PPM, everything after the # is my comment:

magick image.png -compress none ppm:              
P3                            # ASCII PPM signature
3 1                           # dimensions are 3x1 pixels
255                           # pixels are 8-bit scaled
0 0 0 255 255 255 0 255 0     # black pixel, white pixel, lime green pixel

The above syntax is for writing the PPM file on stdout. If you want it written to a file, just do:

magick image.png -compress none result.ppm

Obviously it works for BMPs and all other formats too:

magick image.bmp -compress none result.ppm

When you have finished processing your image with assorted tools, you can convert back to a BMP or other format with:

magick image.ppm result.bmp

Now that we have created a little PNG image with ImageMagick, I can show you how to convert it to ASCII PPM with the NetPBM tools:

pngtopam -plain image.png
P3
3 1
255
0 0 0 255 255 255 0 255 0 

Hopefully you will see that this gives the same result as the code I showed earlier magick image.png -compress none ppm:

Related