Page 1 of 1

Understanding the PGM format

Posted: 2010-08-03T02:36:34-07:00
by coulon
I believe I am capable to write my own image processing in Fortran but I need an ASCII format of my
grayscale image to do this.

The TXT format would be OK but it is verbose and the conversions are slow. So I think the PGM format is what I need, but I couldn't find the right documention.

At which line and column of the PGM file is the pixel that was at some given line and column of my original PNG?

Re: Understanding the PGM format

Posted: 2010-08-03T09:38:39-07:00
by fmw42

Re: Understanding the PGM format

Posted: 2010-08-03T20:39:48-07:00
by anthony
PGM has two formats. Binary (compressed) and Ascii (uncompressed)
After the header is output, each pixel is just listed row by row from top left to bottom right.
There is no 'hard' line structure to the file format. Just white-space separated numbers or binary values.

However if you convert all spaces to newline you can then do line lookup by pixle position.

Re: Understanding the PGM format

Posted: 2010-08-03T20:54:04-07:00
by fmw42
After the header is output, each pixel is just listed row by row from top left to bottom right.
But you have to know how many rows and columns as the PGM format, as I understand it, is not one row in the file for every row in the image, but is limited to some maximum number of entries in the PGM file row. I don't recall how many, but it is not very big.

As Anthony says, you can remove the few header lines and then list out the remaining values as one long list of values and then use the number of rows and columns to figure out what parts to use, if you need to crop, etc, or just process all the pixel data and then put it back into a new PGM image by adding the header back.

However, IM can process PGM images to output them after processing as PGM or any other IM supported format.

Re: Understanding the PGM format

Posted: 2010-08-03T21:06:26-07:00
by anthony
All values are just white-space separated. That is it. That could be spaces tabs or newlines.

Rows do not actually matter unless you format the rows that way.
Even the header can have multiple and variable number of lines though typically it isn't.

I have quite often placed ALL values header and data all on one line, or all on separate lines!
I have even done it in IM examples!

You just need to parse it. for shell parsing, easiest thing is replace all white space with a single return so every value (inclusing the header) is one per line. You can then read the values easilly, or cut off the appropriate number of lines to strip the header.

Re: Understanding the PGM format

Posted: 2010-08-03T22:10:32-07:00
by coulon
anthony wrote:PGM has two formats. Binary (compressed) and Ascii (uncompressed)
Of course I said -compress none to obtain the ASCII format!
anthony wrote: After the header is output, each pixel is just listed row by row from top left to bottom right.
There is no 'hard' line structure to the file format. Just white-space separated numbers or binary values.
Thanks. I succeeded in reading a PGM file and making my own.

Note that Imagemagick writes a PGM file with a variable number of bytes per line: if Maxval=255 there are more bytes if there are many 1- or 2-digit bytes. This provides a nicer looking array, but complicates the reading software.

Re: Understanding the PGM format

Posted: 2010-08-03T22:21:35-07:00
by anthony
Most reading software just read value by value directly using scan() in C code or equivelent.
They don't try to read NetPBM or PBMPlus files line by line. Do that and you will have difficulties!

The same method is also what is used for binary reading of the files!
You could always look at the 'coder' file in ImageMagick :-)


Shells can filter the file stream through 'tr' to make it one value per line.


It isn't hard, if you do it the right way!