Convert Unknown Image Files to PNM with anytopnm
You will turn a supported image file into PBM, PGM or PPM, without having to choose the format-specific Netpbm converter first. The command writes the converted image to standard output, so the practical result is a new file created by shell redirection. Allow about ten minutes for a first conversion. You need the netpbm package and a readable input file.
The route
Jump straight to the step you need, or tick off Done means at the end.
This guide describes Netpbm 11.5.2, installed here as Debian package version 2:11.05.02-1.1build1. The manual page is dated 15 November 2014, so check the installed documentation if your package is substantially different.
1. Check the installed command
Confirm that the shell will run the expected binary and record the package version. These are ordinary, read-only commands and do not need sudo:
$ command -v anytopnm
/usr/bin/anytopnm
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
$ anytopnm -version
pamtopnm: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
...
The version output contains details from the Netpbm library and may include build information. The useful check is that it identifies the expected Netpbm release.
Checkpoint
You have a working /usr/bin/anytopnm and know which package version you are testing.
2. Convert a file to a new output
Replace /path/to/input-image with the path to your image. The destination is an example name, not a fixed output format. Choose the extension that matches the format you expect to receive, but verify the result afterwards:
$ anytopnm /path/to/input-image > converted.pnm
$ file converted.pnm
converted.pnm: Netpbm image data, size = 640 x 480, pixmap, rawbits, binary
The exact file description depends on the input. The converter chooses PBM, PGM or PPM according to the image data. It does not resize the image, and there is no separate output-format option in anytopnm.
Do not run this against a destination you care about without checking it first. Shell > truncates an existing file before anytopnm starts. For a safer replacement, write a temporary file in the same directory and rename it only after checking that conversion succeeded:
$ anytopnm /path/to/input-image > converted.pnm.new
$ test -s converted.pnm.new
$ file converted.pnm.new
$ mv -- converted.pnm.new converted.pnm
The final mv replaces the old destination. If any earlier command fails, leave the original in place and investigate. To recover after an accidental overwrite, restore the destination from your backup or other copy; anytopnm has no undo operation.
3. Use standard input in a pipeline
Pass -, or omit the filename, when the image arrives on standard input. This is useful when another command produces the image or when the source has no useful filename:
$ cat /path/to/input-image | anytopnm - > converted.pnm
$ file converted.pnm
converted.pnm: Netpbm image data, size = 640 x 480, pixmap, rawbits, binary
The program still needs to identify the input. It first uses file, possibly with Netpbm's magic-number data, then falls back to the filename extension. A stream supplied as standard input has no filename extension, so formats that cannot be recognised from their contents may fail. When that happens, use the format-specific Netpbm converter named by the format, such as giftopnm for GIF.
For scripts, make a pipeline failure visible rather than trusting the output file alone:
$ set -o pipefail
$ producer-command | anytopnm - > converted.pnm
$ printf 'pipeline status: %s\n' "$?"
pipeline status: 0
Replace producer-command with a real command that emits image data. Do not paste an untrusted string into a shell pipeline.
4. Let anytopnm handle compressed input
If file identifies Unix compress, gzip or bzip compression, anytopnm uncompresses the input before choosing a converter. The output remains uncompressed PNM data:
$ anytopnm /path/to/input-image.gz > converted.pnm
$ file converted.pnm
converted.pnm: Netpbm image data, size = 640 x 480, pixmap, rawbits, binary
This does not mean every compressed or encoded file is supported. The wrapper still depends on a Netpbm converter for the underlying image format. The manual also documents uuencode and btoa input when file identifies it.
5. Check multiple-image behaviour
Some formats contain several images. In many cases the result is a multi-image Netpbm stream, but the manual warns that some converters keep only the first image. GIF is version-sensitive: Netpbm 10.69 and later convert all images in a multi-image GIF, while earlier releases convert only the first.
Do not assume that a successful exit status means every frame survived. Use a Netpbm inspection tool if it is installed, and test a representative animated or multi-page input before using the command in an archive workflow. The installed package includes pamfile, but the precise report depends on the output and the tool version:
$ pamfile converted.pnm
converted.pnm: Netpbm image data, size = 640 x 480, pixmap, rawbits, binary
6. Diagnose a failed conversion
anytopnm has no command-specific conversion flags. It also does not accept the common libnetpbm options, although -version works. If the command cannot recognise the input, or the required format converter is unavailable, read the error and try the format-specific program. Check the input independently:
$ file /path/to/input-image
/path/to/input-image: PNG image data, 640 x 480, 8-bit/color RGB, non-interlaced
$ anytopnm /path/to/input-image > converted.pnm
$ printf 'status: %s\n' "$?"
status: 0
A non-zero status means the conversion did not complete successfully. Treat a partial output file as unusable and remove or quarantine it before retrying. Removing a generated file is destructive, so inspect the path carefully before using rm. No elevated privileges are normally required for reading an image and writing in your working directory. If permissions fail, fix access to the specific input or destination through your normal system process rather than running the whole pipeline as root.
Done means
anytopnm -versionand the package query identify the installed Netpbm release.- The converted file is written to a deliberate destination and checked with
file. - You know that PBM, PGM or PPM output is selected from the input, not by an anytopnm flag.
- Compressed input and standard-input pipelines are used only when format detection remains reliable.
- Existing output is protected from shell-redirection truncation, and failed conversions are not treated as valid images.
- Multi-image input has been checked when preserving every frame matters.