converting image to pgm plain (text) format

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

converting image to pgm plain (text) format

Post by fmw42 »

How can I convert an image to NetPBM pgm (grayscale) plain/text format.

If I do

convert rose:[10x10+0+0] -colorspace gray -depth 8 rose10.pgm

I get the binary pgm format (P5). I would like to get the plain pgm format (P2).

Is this possible and if so, how to do that.

I tried

convert rose:[10x10+0+0] -colorspace gray -depth 8 P2:rose10.pgm

but still get the binary P5 format.

Thanks

Fred
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: converting image to pgm plain (text) format

Post by magick »

Use -depth 8 or -compress none.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: converting image to pgm plain (text) format

Post by fmw42 »

thanks

convert rose:[10x10+0+0] -colorspace gray -compress none -depth 8 rose10.pgm

P2
10 10
255
47 48 51 52 52 52 50 51 50 50 46 47 49 50 51 50 49 51 51 51 44 44 47 47 45 46
45 46 45 46 45 45 45 45 43 43 43 43 44 44 46 46 46 46 43 43 42 39 42 43 47 47
48 49 49 48 46 43 43 42 50 51 51 54 54 52 50 47 49 46 55 55 56 58 61 60 59 56
56 54 56 57 61 63 64 66 65 65 65 62 54 55 60 64 66 68 68 69 68 63

Unfortunately, I had hoped that the result would show 10 rows of 10 graylevel values each after the 3 header lines. Any way to get that formatted that way with IM or even NetPBM?

I suppose this is just the way NetPBM works. So a loop script will be needed to format all the pixel data into a 10x10 array. Is there a way to do that with printf?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: converting image to pgm plain (text) format

Post by magick »

We'll get a patch into ImageMagick 6.5.9-0 Beta to fix this problem.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: converting image to pgm plain (text) format

Post by fmw42 »

magick wrote:We'll get a patch into ImageMagick 6.5.9-0 Beta to fix this problem.

I am not quite sure what you have in mind. Doing a little research on NetPBM, on the page at http://netpbm.sourceforge.net/doc/pgm.html it says for the plain text format (P2).

"No line should be longer than 70 characters"

So this would limit any formatting to very small subsections of pixels.

Do you know if there is another text format for images that would allow longer lines of text, so that one could have one row of text for each row of the image, no matter what the width of the image might be?

This relates to my attempt to help on this topic viewtopic.php?f=1&t=15312
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: converting image to pgm plain (text) format

Post by magick »

ImageMagick support plain text PBM, PGM, PPM, and TXT formats. None of these produce one row of text per image scanline. Its simple enough to produce the format with one of the language interfaces or script. It would take, for example, about 10 lines of code to produce the format with PerlMagick.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: converting image to pgm plain (text) format

Post by fmw42 »

magick wrote:ImageMagick support plain text PBM, PGM, PPM, and TXT formats. None of these produce one row of text per image scanline. Its simple enough to produce the format with one of the language interfaces or script. It would take, for example, about 10 lines of code to produce the format with PerlMagick.
Yes, thanks. I did not think there were any such image text formats that IM supported. I had already scripted something in unix per my comments on the topic at viewtopic.php?f=1&t=15312. But the person is a Window's user and did not know how to convert my script to DOS Batch. So I thought I would look into a more conducive image format if one exists. Just getting the text in PGM format is some help in reducing the unix commands to extract the data from IM txt: format. The rest is just looping to format the output into lines of uniform number of graylevels. I suspect AWK could do it even more efficient than my crude looping in bash shell script code.

Thanks for your help.
Post Reply