Home / Alt manpages / pnmshear(1)

  • pnmshear(1)
  • User command
  • linux

Shear a PNM Image Safely with pnmshear

You will finish with a PNM image slanted by a measured angle, written to a new file, with the background and colour behaviour understood. The examples use pnmshear from Netpbm 11.5.2, installed here as package version 2:11.05.02-1.1build1.

Allow about ten minutes. You need a readable PNM image and a shell. This workflow changes no system configuration and normally needs no elevated privileges. Keep the original image until you have checked the result.

1. Check the installed command

Confirm the binary and package before building a script around it. These are ordinary, read-only commands:

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

The installed manual describes the syntax as pnmshear [options] angle [pnmfile]. The angle is a floating-point number in degrees. A positive angle shears in one direction and a negative angle shears in the other.

Checkpoint: decide the angle before running the conversion. Avoid values close to 90 or -90 degrees. The output becomes extremely wide, and the program can fail when its internal word sizes cannot represent the required dimensions.

2. Shear into a new output file

Use a new destination so a mistake cannot replace the source. With a real image, substitute its path for /path/to/input.pnm:

$ pnmshear 20 /path/to/input.pnm > /path/to/input-sheared.pnm
$ printf 'exit status: %s\n' "$?"
exit status: 0

With no input filename, pnmshear reads standard input. That makes it useful in a pipeline, but it also makes shell redirection easy to overlook:

$ pnmshear -15 < /path/to/input.pnm > /path/to/input-sheared.pnm

The command writes a PNM image to standard output. A successful exit status means the conversion completed; it does not prove that you selected the right source, angle or destination. The input is not modified by either form.

Warning: > truncates an existing destination before pnmshear starts. If that file matters, choose another name or make a backup first. To replace a file only after a successful run, write to a temporary name and then move it:

$ pnmshear 20 /path/to/input.pnm > /path/to/input-sheared.pnm.new
$ test -s /path/to/input-sheared.pnm.new
$ mv /path/to/input-sheared.pnm.new /path/to/input-sheared.pnm

If the conversion fails, do not run the mv command. Remove the incomplete .new file only after checking its path. The original destination remains available.

3. Verify dimensions and format

Shearing normally changes the width while retaining the image's general height. Inspect the generated file rather than trusting its filename:

$ file /path/to/input-sheared.pnm
/path/to/input-sheared.pnm: Netpbm image data, size = 123 x 80, rawbits, pixmap
$ test -s /path/to/input-sheared.pnm && echo 'output is non-empty'
output is non-empty

Your dimensions and the wording from file will differ. The useful checks are that the file is non-empty, its width is plausible for the chosen angle, and its Netpbm type matches the source's colour or greyscale nature. If you need a visual check, open a copy in an image viewer or convert it with a separate Netpbm tool. Do not edit binary PNM output in a text editor.

Checkpoint: compare the result with the original before deleting anything. A slanted edge, an unexpected blank area or an implausible width usually means the angle, source or background needs review.

4. Choose the background deliberately

The unused area created by the shear needs a background. By default, pnmshear estimates it by averaging the colours of the two top corners. That simple rule can produce a surprising border when those corners are not representative of the image.

Set an explicit background with -background when the output will be composited, displayed or compared against a known colour:

$ pnmshear -background=white 20 /path/to/input.pnm > /path/to/input-white.pnm
$ file /path/to/input-white.pnm

The value is a Netpbm colour name or colour specification accepted by the installed library. Use a value appropriate for the source image. A background colour is part of the output, so it may introduce a colour that was not present in the input.

5. Decide whether to keep anti-aliasing

By default, pnmshear distributes fractions of source pixels across destination pixels. This smooths jagged edges, but it modifies existing colours and commonly creates additional ones. That is usually the better visual result.

Use -noantialias when you need pixels moved without synthesising intermediate values:

$ pnmshear -noantialias -background=white 20 /path/to/input.pnm > /path/to/input-pixel-moved.pnm

This option helps preserve the input palette, but the background still matters. If the background colour is not already in the input, the output cannot contain only the original set of colours. If you only need a smaller palette rather than the exact original colours, the manual points to pnmquant as a separate follow-up step.

Do not assume that -noantialias is automatically superior for screenshots, diagrams or text. Inspect both variants when edge quality matters.

6. Diagnose failures without escalating privileges

If pnmshear cannot open the input, check the path and read permission first:

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

If the output is unexpectedly wide, use a smaller absolute angle and inspect it again. Near-90-degree angles are a design boundary, not a reason to add sudo. Elevated privileges do not fix an invalid image, a bad path or an unrepresentable output size.

Options may be abbreviated to the shortest unique prefix, and the manual permits either a space or an equals sign before an option value. Full option names are clearer in scripts, so prefer -background=white to an abbreviated spelling. Keep filenames quoted when they contain spaces or shell metacharacters.

Done means

  • The installed Netpbm and pnmshear versions were checked.
  • The source image remains untouched and the result was written to a deliberate destination.
  • The angle is comfortably away from 90 and -90 degrees.
  • The output is non-empty and its dimensions and format were inspected.
  • The background was chosen explicitly when the default corner estimate was unsuitable.
  • You selected anti-aliasing or -noantialias based on the required visual or palette result.