Rotate a PNM Image with pnmrotate Without Colour Surprises
Scanning a photo that landed on the flatbed at a stubborn few degrees off square is the ordinary case pnmrotate exists to fix. You will turn a PNM image by a chosen decimal angle, write the result to a separate file, and check its dimensions and format. This is a short job, usually five to ten minutes once Netpbm and the input file are available. The examples use the Netpbm 11.5.2 package installed on this machine.
The route
Jump straight to the step you need, or tick off Done means at the end.
1. Check the input and the command
pnmrotate reads a PNM image and writes a PNM image to standard output. Give it a path when the input is a file, or pipe a PNM stream into it. Start by confirming both the executable and the input description:
$ command -v pnmrotate
/usr/bin/pnmrotate
$ pnmfile /path/to/input.pnm
/path/to/input.pnm: PPM raw, 160 by 120
Replace the path with your own file. Keep the source untouched while testing. The output is binary PNM, so redirect it to a new filename rather than letting it scroll through the terminal.
2. Rotate by an ordinary angle
The angle is measured in decimal degrees counter-clockwise. Negative values are accepted, and the manual recommends staying between -90 and 90 degrees. This example makes a 30 degree copy:
$ pnmrotate 30 /path/to/input.pnm > /path/to/input-rotated-30.pnm
$ pnmfile /path/to/input-rotated-30.pnm
/path/to/input-rotated-30.pnm: PPM raw, 198 by 183
Your dimensions will differ. The result is the smallest rectangle that contains the rotated source, so its width and height normally change. The command itself prints image data, not a progress message; a successful run with a redirection can therefore be silent.
Checkpoint
The new file exists, pnmfile recognises it as PNM, and the original still has its old timestamp and size.
3. Choose the background deliberately
Rotation exposes corners around the source image. Without an explicit setting, pnmrotate estimates the background by averaging the colours at the original image's two top corners. That shortcut is easy to miss and can give a poor result when the top edge is not representative.
Set a named background when the surrounding colour matters:
$ pnmrotate -background=white 30 /path/to/input.pnm > /path/to/input-white-30.pnm
$ pnmfile /path/to/input-white-30.pnm
/path/to/input-white-30.pnm: PPM raw, 198 by 183
The colour is parsed by Netpbm's colour-name handling. Use a value that the local Netpbm installation accepts. If you need the output to contain only colours from a deliberately limited palette, remember that the background is also a colour and must be chosen accordingly.
4. Decide whether anti-aliasing is wanted
By default, the three-shear rotation blends source pixels into destination pixels. Edges look smoother, but the output may contain colours or grey levels that were not present in the input. That is usually the better visual result for photographs and continuous-tone artwork.
Use -noantialias when preserving the source colour set matters more than smooth edges:
$ pnmrotate -noantialias -background=white -45 /path/to/input.pnm > /path/to/input-pixel-45.pnm
$ pnmfile /path/to/input-pixel-45.pnm
/path/to/input-pixel-45.pnm: PPM raw, 204 by 201
This mode simply moves pixels rather than synthesising intermediate values. It runs faster and uses less real memory, although the program still holds a copy of the input and output in virtual memory. Large images can therefore need substantial memory in either mode.
5. Use the right tool for quarter turns
Do not use pnmrotate 90 for a simple quarter turn. The Netpbm manual recommends pamflip for rotations that are multiples of 90 degrees because it is faster and more accurate:
$ pamflip -rotate90 /path/to/input.pnm > /path/to/input-quarter-turn.pnm
$ pnmfile /path/to/input-quarter-turn.pnm
/path/to/input-quarter-turn.pnm: PPM raw, 120 by 160
For an angle greater than 45 degrees, the manual also suggests a 90 degree pamflip first, followed by a smaller reverse rotation. That can produce better results than asking pnmrotate to do the whole large-angle transformation.
6. Diagnose failures without overwriting data
These commands write a new output file. If a path already exists, the shell truncates it before pnmrotate starts, so choose a new name or check first:
$ test ! -e /path/to/input-rotated-30.pnm || { echo "output exists; choose another path"; exit 1; }
$ pnmrotate 30 /path/to/input.pnm > /path/to/input-rotated-30.pnm
$ test -s /path/to/input-rotated-30.pnm && pnmfile /path/to/input-rotated-30.pnm
If the input is missing, unreadable, or not a valid PNM image, investigate the diagnostic on standard error and fix the input path or format. A failure after shell redirection can leave an empty or partial destination. Remove that failed destination only after checking that it is the output you intended to recreate, then rerun with a corrected command. No elevated privileges are required for these examples. If the files are owned by another account, solve the permissions issue rather than running an unreviewed pipeline as root.
Done means
- The original PNM file remains unchanged.
- The angle is explicit and is appropriate for the intended direction.
- The background is explicit when the corner estimate is not safe.
- You chose anti-aliasing or pixel preservation deliberately.
pnmfilerecognises the new output and its dimensions make sense.- A quarter turn uses
pamflipinstead of a general rotation.