Converting grayscale ppm to png or jpg gives only black and white

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
jogchum
Posts: 5
Joined: 2020-02-09T12:52:17-07:00
Authentication code: 1152

Converting grayscale ppm to png or jpg gives only black and white

Post by jogchum »

I have some 1000 grayscale (mandelbrot) files in .ppm-format, which I want to convert to png or jpg. But convert leaves out all the gray, writing only black and white to the .png. (or jpg). I used "-colorspace gray", "-compress none" and "-depth 16" arguments, but the result is always the same.
I'm on OpenSuse Tumbleweed.
convert -v gives
Version: ImageMagick 7.0.9-17 Q16 x86_64 20200118 https://imagemagick.org

Thanks for any help!

User avatar
fmw42
Posts: 26292
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting grayscale ppm to png or jpg gives only black and white

Post by fmw42 »

Post a link to an example of your images to some free hosting service and put the URL here. Also provide your exact command line so we can see and try it.

jogchum
Posts: 5
Joined: 2020-02-09T12:52:17-07:00
Authentication code: 1152

Re: Converting grayscale ppm to png or jpg gives only black and white

Post by jogchum »

The command is
convert -depth 16 -colorspace gray -compress none "pvar-1.500000.ppm" "pvar-1.500000.jpg"

I'll try an find a hosting service to upload the files to.

jogchum
Posts: 5
Joined: 2020-02-09T12:52:17-07:00
Authentication code: 1152

Re: Converting grayscale ppm to png or jpg gives only black and white

Post by jogchum »


User avatar
fmw42
Posts: 26292
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting grayscale ppm to png or jpg gives only black and white

Post by fmw42 »

Your file is rather strange if not malformed. It says it is 9/1-bit image. Imagemagick sees the RGB values as either 0 or 511 so binary.

Code: Select all

  Depth: 9/1-bit
  Channel depth:
    red: 1-bit
    green: 1-bit
    blue: 1-bit
  Channel statistics:
    Pixels: 1470000
    Red:
      min: 0  (0)
      max: 511 (1)
      mean: 497.307 (0.973204)
      standard deviation: 82.5196 (0.161487)
      kurtosis: 32.3466
      skewness: -5.8606
      entropy: 0.17806
    Green:
      min: 0  (0)
      max: 511 (1)
      mean: 497.307 (0.973204)
      standard deviation: 82.5196 (0.161487)
      kurtosis: 32.3466
      skewness: -5.8606
      entropy: 0.17806
    Blue:
      min: 0  (0)
      max: 511 (1)
      mean: 497.307 (0.973204)
      standard deviation: 82.5196 (0.161487)
      kurtosis: 32.3466
      skewness: -5.8606
      entropy: 0.17806
...
 Colors: 2
  Histogram:
   39390: (0,0,0) #000000000000 black
   1430610: (65535,65535,65535) #FFFFFFFFFFFF white
My Mac Preview will display your PPM image properly. But I do not know how to make Imagemagick convert it so that it is properly visible.

I think the Imagemagick developers will need to check this file out. It is possible Imagemagick is not reading the file properly

jogchum
Posts: 5
Joined: 2020-02-09T12:52:17-07:00
Authentication code: 1152

Re: Converting grayscale ppm to png or jpg gives only black and white

Post by jogchum »

Hmm, I create the file myself, in c, according to the ppm format for 16bit samples, three samples per pixel. GwenView renders the file fine, and converts it correct to either jpg or png. But does this on a file-by-file base, so converting 1000 of them is rather cumbersome...

What is the command you use to get the file properties in your answer above?

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

Re: Converting grayscale ppm to png or jpg gives only black and white

Post by magick »

Try this command:

Code: Select all

$ pnmtojpeg pvar-1.500000.ppm pvar-1.500000.jpg
pnmtojpeg: Red sample value 65535 is greater than maxval (256)
This suggests your image file does not follow the PNM specification. We manually changed the max value from 256 to 65535 and your image now renders properly.

jogchum
Posts: 5
Joined: 2020-02-09T12:52:17-07:00
Authentication code: 1152

Re: Converting grayscale ppm to png or jpg gives only black and white

Post by jogchum »

Changing maxval from 256 to 65535 makes convert do the job, indeed, thanks! But is still odd that both Gwenview and Gimp are able to create good jpg's or png's from the original ppm-files.

snibgo
Posts: 12938
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting grayscale ppm to png or jpg gives only black and white

Post by snibgo »

Note that 256 is a very strange maxval, as that needs 9 bits. At one byte/channel/pixel, the expected maxval would be 255. But the file size and stated dimensions suggest there are 6 bytes total per pixel.
snibgo's IM pages: im.snibgo.com

ferdnyc
Posts: 6
Joined: 2014-03-30T10:03:00-07:00
Authentication code: 6789

Re: Converting grayscale ppm to png or jpg gives only black and white

Post by ferdnyc »

jogchum wrote:
2020-02-10T04:50:25-07:00
Changing maxval from 256 to 65535 makes convert do the job, indeed, thanks! But is still odd that both Gwenview and Gimp are able to create good jpg's or png's from the original ppm-files.

I think it comes down to this, from the description of the file format found in the ppm(5) man page:

Code: Select all

       ·      A raster of Height rows, in order from top to bottom.  Each  row
              consists  of  Width  pixels,  in order from left to right.  Each
              pixel is a triplet of red, green,  and  blue  samples,  in  that
              order.  Each sample is represented in pure binary by either 1 or
              2 bytes.  If the Maxval is less than 256, it is 1 byte.   Other‐
              wise, it is 2 bytes.  The most significant byte is first.
Because your maxval is set one higher than the cutoff for 1-byte values, the file technically specifies 2-byte samples, so the "effective maxval" for the encoding is already 65535. (Which was exactly snibgo's point about the maxval.)

The manpage had earlier observed:
It should also be noted that files often conform to this format in every respect except the precise semantics of the sample values.
So tools that interpret PPM most likely do it very liberally, as a general rule, and without paying any attention to the maxval beyond using it to determine the sample bytewidth per the above formula.

ferdnyc
Posts: 6
Joined: 2014-03-30T10:03:00-07:00
Authentication code: 6789

Re: Converting grayscale ppm to png or jpg gives only black and white

Post by ferdnyc »

It should also be pointed out that netpbm even includes a tool to repair corrupted Netpbm images, "pamfix". Correcting problems of this type appears to be one of its primary (practically only) functions:

Code: Select all

       pamfix reads a stream that is mostly a Netpbm image but may  have  cer‐
       tain  types  of corruptions and produces a valid Netpbm image that pre‐
       serves much of the information in the original.

       In particular, Netpbm salvages streams that are truncated and that con‐
       tain illegally large sample values.

Code: Select all

   Excessive Sample Value
       This is a stream that contains a purported sample value that is  higher
       than the maxval of the image.

       The  header of a Netpbm image tells the maxval of the image, which is a
       value that gives meaning to all the sample values in the  raster.   The
       sample  values  represent  a  fraction of the maxval, so a sample value
       that is greater than the maxval makes no sense.

       A regular Netpbm program fails if you give it  input  that  contains  a
       value larger than the maxval where a sample value belongs.

       pamfix has three ways of salvaging such a stream:

       ·      Clip to the maxval.  Request this with -clip.

       ·      Raise  the  maxval,  thus  lowering  the fraction represented by
              every sample in the image.  Request this with -changemaxval.

       ·      Truncate the image at the first invalid sample  value.   Request
              this with -truncate and neither -clip nor -changemaxval.
So I guess you're in good company, at least. 8)

Post Reply