Page 1 of 1

convert jp2 to ascii pgm

Posted: 2011-05-11T08:54:13-07:00
by lukigno82
Hi,
I'm using ImageMagick to convert in a bash script a jp2000 img into an ascii pgm one.
I used the following command:

Code: Select all

convert infile.jp2 -compress none outfile.pgm 
I already obtained an ascii file, but numeric values of the image are in more rows,
ehile I would like to obtain values only one one row like this:

Code: Select all

P2
1000 100
255
1 2 3 4 5 6 7 8 9 ...
Is that possible? Thanks.

Luca

Re: convert jp2 to ascii pgm

Posted: 2011-05-11T18:51:04-07:00
by anthony
PGM in ascii text format is very flexible. IM actually does not output rows, just outputs its buffer when full.

pnmnoraw will output each row of image data on a single line.
See IM Examples. Common File Formats, PbmPlus and NetPBM, for examples..
http://www.imagemagick.org/Usage/formats/#pbmplus

However you asked all on one line. so simple replace returns and newlines with spaces using "tr" text utility (linux and unix), The "tr" command can even compress (suppress) multiple spaces to a single space, to make it easier to parse.

Code: Select all

    convert infile.jp2 -compress none pgm:- | tr -s '\012\015' ' '  > outfile.pgm 
There all one line (without even a final return/newline).
Remember the first few 'words' are not image data but image attributes (magic, size, depth)

Re: convert jp2 to ascii pgm

Posted: 2011-05-12T02:11:34-07:00
by lukigno82
Thank you Antony.
It did work!

Luca