Enlarge a PBM Bitmap with Edge Smoothing Using pbmpscale
A scaled-up bitmap usually turns blocky, but pbmpscale enlarges a PBM and smooths the edges as it grows. It works on a black-and-white image by a whole-number factor, and gives you a new file with predictable dimensions, not a guess. The installed command is Netpbm 11.5.2, from package version 2:11.05.02-1.1build1. Allow about ten minutes. You need a shell, a readable PBM input file, and enough space for the larger output. No step here needs elevated privileges.
The route
Jump straight to the step you need, or tick off Done means at the end.
1. Check the installed command
Confirm which executable will run and read its short usage message:
$ command -v pbmpscale
/usr/bin/pbmpscale
$ pbmpscale
pbmpscale: Use 'man pbmpscale' for help.
pbmpscale: You must specify the scale factor as an argument
The command takes a scale factor first, followed by an optional PBM filename:
pbmpscale N [pbmfile]
There are no pbmpscale-specific switches. Netpbm common options may also be accepted, but do not add one unless you have checked the common-options documentation for the installed Netpbm release.
Checkpoint
The first argument is the factor, not the input path. A command such as pbmpscale image.pbm 2 has the arguments in the wrong order.
2. Enlarge an existing PBM file
Use a new destination name so the original remains available for comparison or recovery:
$ pbmpscale 2 /path/to/input.pbm > input-2x.pbm
- What
2means. A two-times enlargement in both directions. The program reads a PBM image, replicates pixels, and adds smoothing around corners and edges. - Why start there. The manual says a factor of 2 gives the best results, so use it when you want a larger-looking bitmap without changing its monochrome PBM format.
Warning
The output goes to standard output. Shell redirection creates or truncates the destination before the program finishes, so do not redirect over the only copy of a useful image. If you accidentally create a bad new file, remove that new file after checking the path; the input is unchanged.
3. Verify the result and its dimensions
Check the output with the file utility:
$ file /path/to/input.pbm input-2x.pbm
/path/to/input.pbm: PBM image data, 320 x 200
input-2x.pbm: PBM image data, 640 x 400
The wording may vary slightly between versions of file. The useful checks are that the output is recognised as PBM and that each dimension is exactly twice the input. A PBM is a one-bit image, so do not expect a greyscale or colour output merely because the edges have been smoothed.
For a small plain-text PBM, you can also inspect the header directly:
$ head -n 2 input-2x.pbm
P4
640 400
Most Netpbm programs write raw PBM, whose magic number is P4. The remainder is binary data, so do not print the whole file in a terminal or edit it as text.
Checkpoint
Stop here if the dimensions are not the expected factor times the source. Check that the input is the file you intended and that the factor was the first argument.
4. Read from standard input when that fits the workflow
The filename is optional. This lets another command provide the PBM stream:
$ cat /path/to/input.pbm | pbmpscale 2 > input-2x.pbm
- Use a named file when it is easier to review the input directly.
- Use a pipe when the preceding command already produces PBM and you want to skip an intermediate file.
- Keep diagnostics separate from the image stream: a program that writes unexpected text to standard output can corrupt the redirected PBM.
To convert a different image format first, use a suitable Netpbm converter and verify its output before scaling. Do not assume that a file ending in .pbm is valid PBM; the format header and raster still have to be readable.
5. Build larger factors from repeated two-times steps
The manual recommends repeated enlargements by 2 when the total factor is greater than 2. For an eight-times result, use separate files:
$ pbmpscale 2 input.pbm > input-2x.pbm
$ pbmpscale 2 input-2x.pbm > input-4x.pbm
$ pbmpscale 2 input-4x.pbm > input-8x.pbm
$ file input-8x.pbm
input-8x.pbm: PBM image data, 2560 x 1600
Replace the example dimensions with the dimensions of your own input. Each command reads the previous file and writes the next one, so a failed step leaves the earlier checked output available. When the target factor is not a power of two, apply as many two-times steps as possible and then use the remaining positive factor. Check the final dimensions rather than trusting the filenames.
6. Handle failures without guessing
A missing argument produces a usage error. A factor below one is rejected by the installed command:
$ pbmpscale 0 input.pbm > bad-output.pbm
pbmpscale: Scale argument must be at least one. You specified 0
Do not treat a scale factor as a way to reduce an image. This command is for enlargement; use another Netpbm scaler when you need reduction or independent width and height controls.
If the input cannot be opened, check the path and permissions without changing anything:
$ ls -l /path/to/input.pbm
$ file /path/to/input.pbm
If the command reports an invalid PBM, preserve the original and inspect it with the tool that created it. Do not run the converter as root to bypass an ordinary path or permission mistake. If the output file is incomplete, discard only that newly created output and rerun after fixing the input or destination.
Done means
- Installed and known: the installed command is
/usr/bin/pbmpscalefrom Netpbm 11.5.2. - Argument order right: the scale factor was supplied before the input filename.
- Original intact: the original PBM remains intact and the result has a separate filename.
- Dimensions checked:
filerecognises the result as PBM with the expected enlarged dimensions. - Built in steps: any larger-than-two enlargement was built from checked intermediate files where practical.