Home / Alt manpages / pamstretch(1)

  • pamstretch(1)
  • User command
  • linux

Scale PNM Images Smoothly with pamstretch

You will enlarge a PNM or PAM image with interpolated pixels, check the resulting dimensions, and choose deliberately how the right and bottom edges are handled. The examples use pamstretch from Netpbm 11.5.2, installed here as package version 2:11.05.02-1.1build1.

Allow about fifteen minutes. You need a shell, the Netpbm utilities, a readable PNM or PAM input, and enough free space for the output. These are ordinary file conversions, so they normally need no elevated privileges. This guide does not edit system configuration or replace an existing image unless you explicitly choose to do so.

1. Check the installed command and input

Confirm that the command resolves to the program you expect and record the installed Netpbm version:

$ command -v pamstretch
/usr/bin/pamstretch
$ pamstretch --version
pamstretch: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1

The program points help requests at its manual rather than printing a full option screen. Read the installed contract with man pamstretch if an option behaves differently on another host. Then inspect the input without changing it:

$ pamfile /path/to/input.pnm
/path/to/input.pnm: PPM raw, 640 by 480  maxval 255

Your format and dimensions will differ. The useful checkpoint is that pamfile recognises the file and reports its width and height. Fix a path or permission problem before attempting the conversion.

2. Enlarge both dimensions

The positional scale factor is an integer and applies to both axes when neither axis-specific option is present. This example creates a new file, leaving the source untouched:

$ pamstretch 2 /path/to/input.pnm > /path/to/input-2x.pnm
$ pamfile /path/to/input-2x.pnm
/path/to/input-2x.pnm: PPM raw, 1280 by 960  maxval 255

For a 640 by 480 source and factor 2, the normal output is 1280 by 960. pamstretch inserts interpolated rows and columns instead of simply copying neighbouring pixels, which can look smoother than enlargement with pamenlarge. It is still an enlargement filter, not a detail-recovery tool: a small or heavily compressed source cannot supply missing detail.

Checkpoint: compare the reported dimensions with the arithmetic you intended. If you are processing many files, check each output rather than assuming that a successful shell command means the visual result is suitable.

3. Scale one axis only

Use -xscale for horizontal scaling and -yscale for vertical scaling. If you specify only one, the other axis has a scale factor of 1:

$ pamstretch -xscale=3 -yscale=1 /path/to/input.pnm > /path/to/input-wide.pnm
$ pamfile /path/to/input-wide.pnm
/path/to/input-wide.pnm: PPM raw, 1920 by 480  maxval 255

The option values are integers. Do not pass the positional factor together with -xscale or -yscale; the manual defines the positional form only when neither axis-specific option is supplied. For non-integer sizes such as 2.5, use the separate pamstretch-gen program if it is installed and suitable for your workflow.

Option spelling has a small portability trap. pamstretch accepts the minimum unique abbreviation of an option, accepts two hyphens in place of one, and accepts either -xscale=3 or -xscale 3. Full names with an equals sign are clearer in scripts and less likely to become ambiguous if a later version adds an option.

4. Choose an edge policy

Interpolation happens between the top-left corners of the scaled pixels. At the right and bottom edges there is no following pixel to interpolate towards, so the default is asymmetric: the right edge is interpolated vertically and the bottom edge horizontally.

Use -blackedge when you want the interpolation to reach black at the right and bottom edges:

$ pamstretch -xscale=2 -yscale=2 -blackedge /path/to/input.pnm > /path/to/input-black-edge.pnm
$ pamfile /path/to/input-black-edge.pnm
/path/to/input-black-edge.pnm: PPM raw, 1280 by 960  maxval 255

Use -dropedge when you prefer pamstretch to drop one source pixel at those edges. That can be more logical for some interpolation workflows, but the output dimensions become slightly unusual. Verify rather than guessing:

$ pamstretch -xscale=2 -yscale=2 -dropedge /path/to/input.pnm > /path/to/input-drop-edge.pnm
$ pamfile /path/to/input-drop-edge.pnm
/path/to/input-drop-edge.pnm: PPM raw, 1278 by 958  maxval 255

The dimensions above assume a 640 by 480 input and describe the documented edge effect. On a very small image, calculate and inspect the actual result. With a scale factor of 1, Netpbm 11.5.2 keeps the input edges even when -dropedge is present. Older Netpbm releases before 10.86 could drop an edge in that case, so do not rely on that detail when supporting older systems.

5. Protect existing files

Shell redirection with > opens the destination before pamstretch runs and can truncate an existing file. Use a new destination name for a first attempt:

$ pamstretch 2 /path/to/input.pnm > /path/to/input-2x.test.pnm
$ test -s /path/to/input-2x.test.pnm && pamfile /path/to/input-2x.test.pnm
/path/to/input-2x.test.pnm: PPM raw, 1280 by 960  maxval 255

If the command fails, keep the original and remove the incomplete test file only after checking its name. If the conversion succeeds, inspect the image and then choose whether to retain the new name or replace an old output. Replacing a file is a separate, state-changing action:

$ cp --preserve=all /path/to/old-output.pnm /path/to/old-output.pnm.bak
$ mv /path/to/input-2x.test.pnm /path/to/old-output.pnm

This backup gives you a recovery path. To undo the replacement, restore it deliberately with mv /path/to/old-output.pnm.bak /path/to/old-output.pnm, after deciding what to do with the newer output. Do not run a blind batch replacement, and do not use sudo merely because an image conversion failed. Elevated privileges can hide an ownership or path mistake and can create root-owned output.

6. Handle format and quality traps

pamstretch reads PNM or PAM input and writes a Netpbm image stream. Check the output with pamfile before handing it to another program. If a later tool needs PNG or JPEG, perform that conversion as a separate step so a failure is easy to locate.

PBM input is a known weak case. The manual warns that enlarged PBMs often look ugly because their one-bit pixels expose interpolation noise. Reduce that noise first with a suitable tool such as pnmnlfilt, or use a workflow designed for crisp bitmap artwork. Do not assume that interpolation is the right treatment for line art, masks or barcode-like images.

If the output is unexpectedly large, remember that PNM is generally an uncompressed working format. Check dimensions and maxval, then convert or compress only after the image passes visual inspection. Keep the source until the final file has been opened successfully.

Done means

  • The installed command and Netpbm version were checked.
  • The source was recognised as a readable PNM or PAM image.
  • The selected scale produced the dimensions you expected.
  • Any one-axis or edge policy was explicit and its output was verified with pamfile.
  • Existing output was not overwritten accidentally, and a backup exists if you deliberately replaced it.
  • The original image remains available until the enlarged result has been checked.