Convert Common Images to PostScript with imagetops
You will finish with a repeatable command for converting a PNG, JPEG, GIF, BMP or TIFF image into PostScript, including a grayscale variant and a controlled print size. The examples use the imagetops supplied by Netpbm 2:11.05.02-1.1build1 on this machine.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about ten minutes. You need a shell, a readable image file and the netpbm package. The conversion itself does not need elevated privileges. It writes PostScript to standard output, so choose the output file deliberately rather than sending an experiment straight to a printer.
1. Check the installed command
Confirm which executable will run and which package supplied it:
$ command -v imagetops
/usr/bin/imagetops
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
The local manual describes imagetops as a two-stage filter. It identifies the image format, invokes the matching Netpbm converter to produce PNM data, then invokes pnmtops to produce PostScript. The supported formats listed by this installed manual are JPEG, PNG, X-PNG, BMP, X-BMP, GIF and TIFF.
Checkpoint: if command -v prints nothing, stop and install or repair the package through your normal system administration process. Do not work around a missing executable by downloading an unrelated script.
2. Convert a file to PostScript
Use a real input path and redirect standard output to a new file:
$ imagetops /path/to/input.png > output.ps
pnmtops: generating color Postscript program.
The diagnostic is normally written to standard error, while the PostScript document goes into output.ps. Check both the exit status and the start of the file:
$ printf 'exit status: %s\n' "$?"
exit status: 0
$ head -5 output.ps
%!PS-Adobe-3.0 EPSF-3.0
%%LanguageLevel: 2
%%Creator: pnmtops
%%Title: noname.ps
%%Pages: 1
The exact header can vary with the input and the options passed to pnmtops. A successful status means the pipeline completed; it is still sensible to inspect the result with a PostScript-aware viewer or test printer before using it in a production print job.
Do not use a path that names a directory or a missing file by accident. The wrapper treats a missing final file as input from standard input, which can make a typo look like a command that is waiting for data.
3. Convert standard input
When the image is arriving from a pipe, leave out the filename:
$ cat /path/to/input.png | imagetops > output-from-stdin.ps
pnmtops: generating color Postscript program.
$ head -1 output-from-stdin.ps
%!PS-Adobe-3.0 EPSF-3.0
imagetops stores standard input in a temporary file before it identifies the format. The manual says it uses $TMPDIR, or /tmp when that variable is unset. This is ordinary temporary data, but treat the directory as part of your operational environment: check available space when converting unusually large images and avoid placing confidential images in a shared temporary location without an appropriate host policy.
For a pipeline, preserve the converter's status instead of only checking the final redirection:
$ set -o pipefail
$ cat /path/to/input.png | imagetops > output-from-stdin.ps
$ printf 'pipeline status: %s\n' "$?"
pipeline status: 0
That status is meaningful only in a shell where pipefail is supported and enabled. In a script, check the status in the shell's documented way.
4. Request grayscale output
Add -gray when the PostScript should contain grayscale image data:
$ imagetops -gray /path/to/input.png > output-grey.ps
$ printf 'exit status: %s\n' "$?"
exit status: 0
$ head -3 output-grey.ps
%!PS-Adobe-3.0 EPSF-3.0
%%LanguageLevel: 1
%%Creator: pnmtops
On this implementation, the option inserts a PNM colour-to-gray conversion before pnmtops. It does not change the original image. Compare the resulting file in a viewer if the distinction matters; a PostScript header is a useful format check, not a visual proof.
There is nothing to undo in this example unless you overwrite an existing output file. To recover from an accidental overwrite, restore that file from your backup or regenerate it from the original image. Use a new destination while testing.
5. Pass page and size options to pnmtops
Arguments other than -gray are passed to pnmtops. For example, request a two-inch image width:
$ imagetops -imagewidth=2 /path/to/input.png > output-two-inch.ps
$ printf 'exit status: %s\n' "$?"
exit status: 0
The important argument order is the options first and the image filename last. The wrapper's filename parsing relies on that shape. Useful forwarded options include -imagewidth, -imageheight, -scale, -dpi, -width, -height, -turn and -noturn. The full pnmtops manual also documents page positioning controls.
Do not combine incompatible sizing modes casually. The local pnmtops manual says that -imagewidth and -imageheight cannot be combined with -scale or -equalpixels. Its default page is 8.5 by 11 inches, and its default scaling is approximately 72 input pixels per inch when the image fits. If you need exact layout, state the dimensions explicitly and test the rendered page.
Quote paths that contain whitespace:
$ imagetops -imagewidth=2 "/path/to/Project scan.png" > "Project scan.ps"
6. Diagnose failures without changing the system
For a file that is not recognised as an image, the wrapper exits non-zero and reports Not an image. For a recognised but unsupported MIME type, it reports Unsupported image type. Check the file before retrying:
$ file --brief --mime-type /path/to/input.png
image/png
$ imagetops /path/to/input.png > output.ps
$ printf 'status: %s\n' "$?"
status: 0
If the file is valid but the conversion stage fails, run the corresponding Netpbm converter separately, then feed its output to pnmtops. For a PNG, that diagnostic split is:
$ pngtopnm /path/to/input.png > /tmp/input.pnm
$ pnmtops /tmp/input.pnm > /tmp/input.ps
$ head -1 /tmp/input.ps
%!PS-Adobe-3.0 EPSF-3.0
The files under /tmp are temporary diagnostic artefacts. Remove them using your normal temporary-file housekeeping after inspection, and do not use this debugging path for sensitive images on a host where temporary storage is not suitably protected.
Done means
- You confirmed the installed Netpbm version and executable.
- A supported image produced a PostScript file with exit status 0.
- You know that
-graychanges the conversion pipeline, not the source image. - You can pass compatible sizing options to
pnmtopswith the input filename last. - You checked the MIME type and separated format errors from downstream conversion errors.
- You tested output files before sending them to a printer or replacing a trusted document.