Page 2 of 2

Re: Convert GIF (or PNG or JPG) to a 4-bit PGMA

Posted: 2010-11-18T11:45:39-07:00
by fmw42
magick wrote:Add '-compress none' to your command line.
I think he might mean prior to PGM:-

But I am not an expert on this. Anthony is the real expert on NetPBM

Re: Convert GIF (or PNG or JPG) to a 4-bit PGMA

Posted: 2010-11-18T18:12:51-07:00
by anthony
NetPBM images have two forms.. Ascii text images, and binary 'raw' images.

First the newer floating point format PFM is only available in 'raw' binary format :-(

As for the other formats. you can generate them in two way.
First by default ALL IM and NewPBM commands (they are separate libraries and packages) output the smalle binary 'raw' formats by default.

IM will generate a text format is you specify -compress none on the command line (generally just before outputing the result where the setting is used).

NetPBM (separate package) has a command "pnmnoraw" which converts a binary image into a text (ascii) version. It is usually used as the last item in the NetPBM image 'pipeline' (which is the way NetPBM processes images).

"pnmnoraw: command not found" ; the command is part of the NetPBM package, a former rival of IM.


The NetPBM image file formats are very simple, so simple you don't need the actual NetPBM, PBM, or PBMPlus, (other names for this package) library to read, write, or process the images. That was the point of the image file format. Its simple uncomplicated nature. It is also why it is still such a useful image file format.

Re: Convert GIF (or PNG or JPG) to a 4-bit PGMA

Posted: 2010-11-19T09:59:40-07:00
by Raconteur
Ok, thanks, Anthony. But... how do I take this 8-bit GIF with 16 gray-shades in it and turn it into a 4-bit PGM where 0 = black, 15 = white and the remaining grays map to the intermediate values? I need the output file to be readable by a text editor. Can you give me the magic command line? So far, nothing I have tried works without hand-modification to the resultant PGM.

Thanks a million!

Re: Convert GIF (or PNG or JPG) to a 4-bit PGMA

Posted: 2010-11-19T11:30:58-07:00
by fmw42
Anthony wrote:IM netpbm output is limited to 8 and 16 bit depth. setting the Maxval Netpbm setting is an option i would like to see added to the netpbm coder.
So I think you still need to manually set the max value.

Re: Convert GIF (or PNG or JPG) to a 4-bit PGMA

Posted: 2010-11-19T11:40:37-07:00
by Raconteur
I figured as much, but it seemed as though we were getting to a way of doing it. Even if I could do the -evaluate divide 17 trick, and then in another convert statement change the maxgray value, I could put that into a script and have my people run that.

Re: Convert GIF (or PNG or JPG) to a 4-bit PGMA

Posted: 2010-11-19T12:48:28-07:00
by fmw42
Raconteur wrote:I figured as much, but it seemed as though we were getting to a way of doing it. Even if I could do the -evaluate divide 17 trick, and then in another convert statement change the maxgray value, I could put that into a script and have my people run that.
Anthony is best to consult about this. He pointed out earlier that IM is limited to setting the max value to 2^N-1.

But seems that you should be able to use unix or windows to edit that entry. It is just a text file once it gets into PGM (ascii rather than binary format). In unix you can pipe pipe IM results to other unix commands that will allow you to edit that entry such as SED. I don't know about windows, if that is what you are using.

Re: Convert GIF (or PNG or JPG) to a 4-bit PGMA

Posted: 2010-11-21T19:40:12-07:00
by anthony
Hmmm I tried to output a PGM image at depth '4' and it didn't work...

Code: Select all

convert -size 1x16 gradient: -compress none -depth 4 pgm:-  
P2
1 16
255
255 238 221 204 187 170 153 136 119 102 85 68 51 34 17 0
As you can see the result was depth 8 (maxval = 2^8-1 => 255)

As such it looks like you will need to use one of the techniques now listed in IM examples.
Commond Image File Formats, NetPBM
http://www.imagemagick.org/Usage/formats/#netpbm

for example...

Code: Select all

convert -size 1x16 gradient: +depth +level 0,15 -compress none PGM:-
P2
1 16
65535
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Then replace the third line with a new maxval value of 15

OR do it using NetPBM supplied commands (less accurite in non 2^N cases) ...

Code: Select all

convert -size 1x16 gradient: -depth 16 PGM:- | pamdepth 15 | pnmtopnm -plain
P2
1 16
15
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0