Repair a Truncated Netpbm Image with pamfixtrunc
pamfixtrunc salvages a Netpbm image whose final rows are missing. It reads the usable rows, writes a new image with a corrected height, and leaves the damaged input alone if you direct the output to a separate file. This guide shows the safe workflow and the replacement command to use in new scripts.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about 10 minutes. You need Netpbm installed, a readable PGM, PPM or PAM file, and enough space for a second copy of the recovered image. The examples use a PGM file, but the command accepts Netpbm input rather than a filename extension.
Checkpoint 1: confirm what you are running
- Check the installed commands. This does not need elevated privileges.
command -v pamfixtrunc
command -v pamfix
dpkg-query -W -f='${Version}\n' netpbm
On the system used for this guide, the package is Netpbm 2:11.05.02-1.1build1, with both commands in /usr/bin. Your distribution may report a different package version.
The installed pamfixtrunc is a compatibility wrapper. It invokes pamfix and adds -truncate. That is why diagnostics can name pamfix even when you started pamfixtrunc. Netpbm 10.66, released in March 2014, replaced the old program with pamfix. Keep the old name when an existing script still requires it, but use pamfix -truncate for new work.
Checkpoint 2: test the source without changing it
- Ask pamfile whether the image is too small. Replace the example path with the damaged file. This is a read-only check.
pamfile --allimages /path/to/damaged-image.pgm
A complete image is described with its format, dimensions and maximum sample value. A file that is shorter than its header declares causes pamfile --allimages to fail with an error saying that it could not read the whole image. A successful check does not prove that the picture is visually correct; it only answers the size question.
Do not confuse a truncated file with a valid image that you deliberately want to crop. For a valid image, use pamcut. pamfixtrunc assumes that unreadable data means the bottom of the image is missing, so it can produce a valid file containing a damaged or incomplete picture when the real fault is elsewhere.
Checkpoint 3: write a recovered copy
- Redirect the repaired image to a new pathname. Ordinary user privileges are enough when both files are in a directory you can write.
pamfixtrunc /path/to/damaged-image.pgm > /path/to/recovered-image.pgm
The command reads standard input or the filename supplied on the command line and writes the repaired Netpbm image to standard output. It does not edit the source file. The output header records only the complete rows that could be read, and any partial final row is omitted.
There is one shell trap here: never redirect to the same pathname as the input. The shell opens the output before the program reads its input, which can empty the source before recovery starts. If you accidentally create a bad output, remove only that newly created output and repeat the command with another name. Do not use sudo unless the source or destination directory actually requires it; elevated privileges do not improve image recovery.
For a script that has already migrated to the replacement interface, use the equivalent command:
pamfix -truncate /path/to/damaged-image.pgm > /path/to/recovered-image.pgm
Both forms produce the same truncated-image repair. The replacement also has modes for other corruptions, so do not add those modes casually. In particular, -clip changes excessive pixel samples and -changemaxval changes the declared maximum sample value. They solve different problems from a file that ends early.
Checkpoint 4: verify the result
- Inspect the recovered image metadata.
pamfile --allimages /path/to/recovered-image.pgm
For a two-row source with a two-by-two header but only one complete row, a successful repair reports dimensions of two by one. It should no longer complain that the image is too small. Compare the reported dimensions with the number of rows you expected to survive, and open the image with a trusted viewer if visual content matters.
- Check the command status in automation. A zero exit status means the conversion command completed, not that the recovered picture is aesthetically correct.
if pamfix -truncate /path/to/damaged-image.pgm > /path/to/recovered-image.pgm; then
pamfile --allimages /path/to/recovered-image.pgm
else
printf '%s\n' 'Image recovery failed' >&2
exit 1
fi
Keep the original until this verification and any downstream import have succeeded. The repair is a one-way interpretation of the remaining bytes. It cannot recreate rows that were never written, and it cannot determine whether an interrupted writer, a storage fault or a generator bug caused the short file.
When the output is still wrong
If pamfixtrunc reports an error, first check permissions, available space and whether the input is actually a Netpbm image. A file can be unreadable because it has an invalid header, not because it is merely truncated. Run pamfile --allimages before trying another repair mode.
If the image contains invalid sample values rather than missing tail data, choose the policy deliberately with pamfix. -clip limits samples to the header's maximum; -changemaxval raises that maximum; -truncate stops at the first invalid sample when neither of those options is selected. Do not overwrite the first recovered copy while comparing these results.
For a script migration, replace:
pamfixtrunc input.pgm > output.pgm
with:
pamfix -truncate input.pgm > output.pgm
Retest the script against a known complete image and a deliberately short copy. Netpbm 10.66 also changed how invalid sample values are treated while reading, so results from much older Netpbm releases are not a reliable compatibility test for current installations.
Done means
- The source file is still present and has not been overwritten.
- The repair command completed with status zero and wrote a separate output.
pamfile --allimagesreports the recovered dimensions without a short-file error.- You have checked whether lost bottom rows are acceptable for the next step.
- New scripts use
pamfix -truncate; old scripts are migrated only after testing.