ImageMagick

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?".

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

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?

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.

· 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.

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:

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.

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.