Home / Alt manpages / pnmshear(1)

  • pnmshear(1)
  • User command
  • linux

Shear a PNM Image Safely with pnmshear

pnmshear slants a PNM image by an angle you give it, which is the trick behind fixing a photo scanned slightly crooked. It writes the result to a new file so the original stays intact. 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 gives the syntax as pnmshear [options] angle [pnmfile]. The angle is a floating-point number in degrees: positive shears one way, negative the other.

Checkpoint

Decide the angle before running anything. Stay well clear of 90 or -90 degrees: the output becomes extremely wide, and the program can fail outright 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 touch the source. Substitute a real 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 instead, which is handy in a pipeline but makes it easy to lose track of where redirection is pointing:

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

A zero exit status only means the conversion completed; it says nothing about whether you picked the right source, angle or destination. Neither form touches the input file.

Warning

> truncates an existing destination before pnmshear even starts. If that file matters, pick another name or back it up first. To replace a file only after a successful run, write to a temporary name and move it into place:

$ 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, skip the mv entirely. Delete the incomplete .new file once you have confirmed its path; the original destination is still sitting there untouched.

3. Verify dimensions and format

Shearing widens the image while roughly keeping its height. Inspect the generated file rather than trusting the filename you gave it:

$ 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 own numbers and wording from file will differ. What matters: the file is non-empty, the width is plausible for the angle you chose, and the Netpbm type matches the source's colour or greyscale nature. For a visual check, open a copy in an image viewer or convert it with another Netpbm tool; never edit binary PNM output in a text editor.

Checkpoint

Compare the result with the original before deleting anything. A slanted edge, an unexpected blank patch, or an implausible width usually points back to the angle, the source, or the background choice below.

4. Choose the background deliberately

Shearing opens up unused space that needs a fill colour. By default, pnmshear averages the two top corners to guess one, which can produce a surprising border when those corners are not representative of the rest 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 spec accepted by the installed library. Bear in mind that a chosen background is part of the output, so it can introduce a colour that was never in the input.

5. Decide whether to keep anti-aliasing

By default, pnmshear spreads fractions of source pixels across destination pixels. That smooths jagged edges, usually for the better, but it also modifies existing colours and adds new ones.

Use -noantialias when pixels should move without synthesising anything new:

$ pnmshear -noantialias -background=white 20 /path/to/input.pnm > /path/to/input-pixel-moved.pnm
  • Preserves the input palette, though the background colour still counts: if it was not already in the input, the output cannot contain only the original colours.
  • Want a smaller palette instead of the exact original colours? The manual points to pnmquant as a separate follow-up step.
  • Do not assume -noantialias wins by default 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, try a smaller absolute angle and look again. A near-90-degree angle is a design boundary, not a job for sudo: elevated privileges fix nothing about an invalid image, a bad path, or a size the program cannot represent.

Options can be abbreviated to the shortest unique prefix, and the manual accepts either a space or an equals sign before a value. Full option names read better in scripts, so prefer -background=white over a shortened spelling, and keep filenames quoted when they contain spaces or shell metacharacters.

Done means

  • Version checked: the installed Netpbm and pnmshear versions are known.
  • Source protected: the original image is untouched and the result went to a deliberate destination.
  • Angle sane: comfortably away from 90 and -90 degrees.
  • Output verified: non-empty, with dimensions and format inspected.
  • Background chosen: explicitly, whenever the default corner-average guess would not do.
  • Anti-aliasing decided: on or off, based on the visual or palette result you actually need.