Convert Netpbm Images to Targa with pamtotga
pamtotga turns a PBM, PGM, PPM or PAM image into a TrueVision Targa file, the ancient format that old game tools and asset pipelines still insist on. Budget fifteen minutes: you will pick a sensible Targa representation and check what actually got written, rather than trusting the defaults. The examples use Netpbm 2:11.05.02-1.1build1, the package version installed on this machine.
The route
Jump straight to the step you need, or tick off Done means at the end.
- You need: a shell, the
netpbmpackage, and an input image in a Netpbm format. - No root needed: the conversion itself does not need elevated privileges.
- Write somewhere safe: use a directory where your user can create files, and do not overwrite an original until you have inspected the result.
1. Check the installed command
Confirm the package and executable before relying on option details. This is a read-only check:
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
$ command -v pamtotga
/usr/bin/pamtotga
The local manual describes pamtotga as a converter that reads PBM, PGM, PPM or PAM and writes Targa to standard output. That output design is handy in scripts, but it also makes shell redirection significant: a typo in the destination can leave a partial file if conversion fails.
Checkpoint
You have confirmed the executable and know which package version supplies it.
2. Convert a normal image to a new file
Pass the input path as the optional final argument and redirect standard output to a new Targa file. This keeps the source image untouched:
$ pamtotga /path/to/input.ppm > /path/to/output.tga
For a quick smoke test, a small PPM is enough:
$ printf 'P3\n2 1\n255\n255 0 0 0 0 255\n' > /tmp/two-colours.ppm
$ pamtotga /tmp/two-colours.ppm > /tmp/two-colours.tga
$ file /tmp/two-colours.tga
/tmp/two-colours.tga: Targa image data - Map (2) - RLE 2 x 1 x 8 "/tmp/two-colours"
The exact file description is implementation-dependent, but it should identify Targa and the expected dimensions. Here is the surprise: the default is not 24-bit RGB. pamtotga picks the most constrained compatible type first, monochrome, then colour-mapped, then unmapped colour, and it uses run-length encoding unless you turn it off.
Checkpoint
The command exits successfully, the destination exists, and file identifies a Targa image.
3. Request 24-bit RGB output
- Use
-rgbwhen the consumer expects an unmapped 24-bit colour Targa. - Add
-norlewhen the reader cannot handle run-length encoded Targa files. - Pick one: only one of
-mono,-cmapand-rgbmay be selected.
$ pamtotga -rgb -norle /path/to/input.ppm > /path/to/output-rgb.tga
$ file /path/to/output-rgb.tga
/path/to/output-rgb.tga: Targa image data - RGB 2 x 1 x 24
-rgb and -norle change the output representation, not the source pixels. If you need predictable interoperability, making the representation explicit beats relying on the automatic choice. Do not assume smaller is always better either: run-length encoding usually shrinks the file, but the manual calls out readers that cannot read it. Test the result with the actual program or workflow that will consume it.
4. Use a palette for images with few colours
-cmap requests a colour-mapped Targa. The input must contain no more than 256 distinct colours and must not contain transparency:
$ pamtotga -cmap /path/to/logo.ppm > /path/to/logo-palette.tga
pamtotga: computing colormap...
pamtotga: 2 colors found.
Those two lines are the program's own diagnostic while it builds the palette. A palette can make a simple logo or icon smaller, but it is a poor fit for photographs or gradients with many colours. Go over 256 distinct colours and conversion with -cmap fails outright rather than silently quantising the image, so prepare a reduced-colour input with a suitable Netpbm tool if a palette is a hard requirement.
-cmap16 has the same restrictions but stores full-colour palette entries as 16-bit values, five bits per RGB component. It arrived in Netpbm 10.85, so the installed 11.05.02 package has it.
5. Convert grayscale or monochrome input
Use -mono for PBM, PGM, or PAM input whose tuple type is BLACKANDWHITE or GRAYSCALE:
$ pamtotga -mono /path/to/mask.pgm > /path/to/mask.tga
$ file /path/to/mask.tga
/path/to/mask.tga: Targa image data - Mono - RLE 2 x 1 x 8
This option requests an 8-bit monochrome Targa. It will not turn an RGB image grayscale for you. If the input tuple type does not fit, fix the input first or drop -mono and let pamtotga pick a compatible output itself.
6. Set the Targa image ID
Use -name=name to place an image ID in the Targa header:
$ pamtotga -rgb -name=preview /path/to/input.ppm > /path/to/preview.tga
$ file /path/to/preview.tga
/path/to/preview.tga: Targa image data - RGB 2 x 1 x 24 "preview"
pamtotga truncates the name when the Targa format requires it. Leave the option out and the default name comes from the input filename up to its first period; feed it from standard input and pamtotga skips the image ID entirely. Set an explicit name whenever downstream tooling displays or checks this metadata.
7. Handle transparency carefully
Transparency needs PAM input with tuple type RGB_ALPHA. pamtotga then writes a 32-bit Targa with an extra byte per pixel, and the manual warns that support for such files outside pamtotga is uncertain, so check the whole receiving workflow before shipping this format.
Palette output cannot be combined with transparency. A failed test makes the point:
$ pamtotga -cmap /path/to/rgba-image.pam > /tmp/rgba-palette.tga
pamtotga: Can't do a colormap because image has transparency information
Warning
Do not "fix" this by dropping the alpha channel without deciding what transparent pixels should become. Choose RGB output only when losing transparency is acceptable, or keep the RGB_ALPHA PAM and test a real 32-bit Targa consumer.
8. Verify failures and recover safely
Capture the exit status whenever conversion is part of a script. A successful shell redirection alone does not prove the converter succeeded:
if pamtotga -rgb -- /path/to/input.ppm > /path/to/output.tga; then
printf '%s\n' 'Targa conversion completed'
else
status=$?
printf 'pamtotga failed with status %s\n' "$status" >&2
exit "$status"
fi
- Double hyphen is optional here. It makes the end of options explicit when the installed Netpbm option parser supports it, but an ordinary absolute path does not need it.
- Treat a failed output as untrusted. If conversion fails after creating a file, replace it with a fresh destination once you have fixed the input or options.
- Never touch the source during recovery. Do not delete the source image as part of cleaning up a failed run.
There is no persistent state to undo: pamtotga reads the source and writes the requested output. Recovery just means removing or replacing the failed destination, after you have confirmed its exact path.
Done means
- Checked the installed Netpbm version and confirmed the executable.
- Converted a supported input without overwriting its source.
- Chose RGB, palette, monochrome, compression and image ID deliberately, not by default.
- Checked the output with
fileor the real consuming application. - Treated transparency as a compatibility boundary and checked exit status in scripts.