convert jp2 to ascii pgm

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
lukigno82
Posts: 2
Joined: 2011-05-11T08:41:07-07:00
Authentication code: 8675308

convert jp2 to ascii pgm

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: convert jp2 to ascii pgm

Post 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)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
lukigno82
Posts: 2
Joined: 2011-05-11T08:41:07-07:00
Authentication code: 8675308

Re: convert jp2 to ascii pgm

Post by lukigno82 »

Thank you Antony.
It did work!

Luca
Post Reply