Home / Alt manpages / pnmscalefixed(1)

  • pnmscalefixed(1)
  • User command
  • linux

Scale PNM Images Quickly with pnmscalefixed

You will resize a PNM image to a chosen width and height, check the generated header, and understand when the speed-oriented fixed-point arithmetic can distort an edge. Allow about ten minutes if Netpbm is already installed. The examples are read-only with respect to the input image; only the output path is changed.

1. Check the installed command

This guide uses the Netpbm command installed on this machine. The package is Netpbm 11.5.2, from Debian package version 2:11.05.02-1.1build1. The manual page is dated 1 July 2020, so verify the local manual when moving the command to another release.

$ command -v pnmscalefixed
/usr/bin/pnmscalefixed
$ pnmscalefixed --version
pnmscalefixed: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
pnmscalefixed: Built from source dated 2024-03-31 09:09:47

The command is a fast alternative to pamscale. It uses 12-bit fixed-point arithmetic instead of floating point, which can be quicker but less accurate. Use pamscale when its extra precision or features matter more than this speed trade-off. In particular, pnmscalefixed does not provide pamscale's -nomix option.

2. Scale to exact output dimensions

Use -xsize and -ysize, also available as -width and -height, when you know the required output dimensions. This example halves a four-column test image and doubles its height:

$ pnmscalefixed -xsize 2 -ysize 6 /path/to/input.pgm > /path/to/output.pgm

Both dimensions are independent when both are supplied. That means the aspect ratio can change. If preserving the source shape matters, calculate a matching dimension first, then pass both values explicitly. Do not assume that a width-only request and a height-only request have the same default behaviour as scale factors.

Checkpoint: inspect the result before using it elsewhere.

$ file /path/to/output.pgm
$ head -n 3 /path/to/output.pgm
P5
2 6
255

Netpbm may choose a binary PNM encoding, so file is safer than reading the whole image in a terminal. The useful checks are a non-empty output, the expected dimensions, and a valid PNM header. Do not treat the pixel bytes after the header as text.

3. Use a scale factor when the ratio is the requirement

Use -xscale and -yscale for separate horizontal and vertical factors. A factor of 2 doubles a dimension; 0.5 halves it:

$ pnmscalefixed -xscale 0.5 -yscale 0.5 /path/to/input.pgm > /path/to/half-size.pgm
$ file /path/to/half-size.pgm
/path/to/half-size.pgm: Netpbm image data, size 640 x 480, rawbits, greymap

The displayed dimensions are an example of the expected shape, not a promise about your input. If you supply only one scale factor, the other dimension remains unchanged. If you supply only one absolute dimension, the other dimension is adjusted to preserve the aspect ratio. Supplying both absolute dimensions deliberately overrides that preservation rule.

The older -xysize option and the -pixels option are also documented. Use the full option names in scripts so that a later reader can see the intended operation. Read the installed manual before relying on less common forms.

4. See the fixed-point calculation

Add -verbose when you need to understand what the command actually calculated. Diagnostics go to standard error, while the scaled image still goes to standard output:

$ pnmscalefixed -xsize 2 -ysize 6 -verbose /path/to/input.pgm > /path/to/output.pgm
pnmscalefixed: Scaling by 2048/4096 = 0.500000 horizontally to 2 columns.
pnmscalefixed: Scaling by 8192/4096 = 2.000000 vertically to 6 rows.

The exact message includes your input and output dimensions. The denominator, 4096, reflects the command's 12-bit fixed-point arithmetic. When a requested factor is an exact multiple of 1/4096, the manual says there is no fixed-point distortion. Doubling and halving are exact examples. Other ratios can make one input edge pixel occupy slightly more output space, usually at the right or bottom edge.

This is a precision boundary, not a failed conversion. For a thumbnail or ordinary preview it may be invisible. For measurement, repeated resampling, or an image where edge placement is significant, compare the result with pamscale and choose the version that meets the accuracy requirement.

5. Protect an existing output file

Shell redirection with > truncates its destination before pnmscalefixed starts. That is destructive if the destination already contains a useful image. Write to a temporary name in the same directory, then replace the old file only after checking the new one:

$ pnmscalefixed -xsize 640 -ysize 480 /path/to/input.pgm > /path/to/output.pgm.new
$ test -s /path/to/output.pgm.new
$ file /path/to/output.pgm.new
$ mv /path/to/output.pgm.new /path/to/output.pgm

The mv step changes the destination and can replace an existing file. Confirm the path twice before running it. If conversion fails or the dimensions are wrong, leave the original in place and remove the .new file with your normal file-management process. If the final replacement was accidental, recovery requires restoring the original from a backup or snapshot; pnmscalefixed has no undo operation.

These image conversions normally need no elevated privileges. Use sudo only if the input or output directory is genuinely inaccessible to your user, and check the path before granting a command access to it.

6. Diagnose the common failures

An error opening the input usually means the path is wrong or the file is not readable. Check without changing anything:

$ ls -l /path/to/input.pgm
$ test -r /path/to/input.pgm && echo readable

If the output header has unexpected dimensions, check whether you mixed up absolute sizes and scale factors. -xsize 640 requests 640 columns; -xscale 640 requests a factor of 640, which is a very different operation. If the image looks stretched, both absolute dimensions may have been supplied without preserving the source ratio.

If only the last edge looks slightly displaced, rerun with -verbose and compare the factors. For a precision-sensitive result, run the same dimension request through pamscale and inspect both images. Keep the original input until the chosen output has been checked.

Done means

  • pnmscalefixed is installed and its local Netpbm version is known.
  • The input remains untouched and the output header has the intended dimensions.
  • You chose absolute dimensions or scale factors deliberately, with aspect-ratio consequences understood.
  • -verbose was used when fixed-point precision or edge distortion mattered.
  • An existing output was protected from accidental truncation and replacement.