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

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?".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

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

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

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Raconteur

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

Post 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!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
Raconteur

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

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

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

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

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply