Home / Alt manpages / psidtopgm(1)

  • psidtopgm(1)
  • User command
  • linux

Extract a PostScript Image into PGM with psidtopgm

psidtopgm turns raw hexadecimal image data from a PostScript readhexstring section into a greyscale PGM file. This guide covers running it and then verifying the header and dimensions rather than trusting the output blindly. Allow about fifteen minutes if you already know the image width, height and sample depth. You need the Netpbm package and a shell. This guide uses the installed Netpbm 11.5.2 binary from package 2:11.05.02-1.1build1.

Checkpoint

This is a narrow decoder, not a PostScript interpreter. It does not read a complete PostScript document, discover an image's dimensions or execute PostScript. If you have a normal PostScript file, use pstopnm instead when it is available. Use psidtopgm when you have manually isolated the hexadecimal data for one simple image.

1. Confirm the installed command

Check the binary and its package before preparing data. These are read-only commands and do not need elevated privileges:

$ command -v psidtopgm
/usr/bin/psidtopgm
$ psidtopgm --version
psidtopgm: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
psidtopgm: Built from source dated 2024-03-31 09:09:47
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1

The version output contains build details as well as the library version. A different distribution may show different packaging text. The manual page installed with the command is dated 2 August 1989, and describes this utility as obsolete in favour of pstopnm.

2. Isolate only the image data

Open the PostScript source and identify the data supplied to its readhexstring operation. Copy that hexadecimal stream into a separate text file, without the PostScript operators, comments or closing syntax. Keep the original document unchanged.

For a safe, reproducible smoke test, create a four-sample stream instead:

$ printf '%s\n' '00 01 02 03' > /tmp/psidtopgm-data.txt
$ od -An -tc /tmp/psidtopgm-data.txt
   0   0       0   1       0   2       0   3  \n

The spaces and newline are harmless for this input format. With real data, do not assume that a PostScript file's page size is the image size. The three required numbers describe the extracted image: columns, rows and bits per sample.

3. Convert a small 8-bit image

Run the decoder with the width, height and sample depth before the input filename. The filename is optional; omitting it makes the command read standard input.

$ psidtopgm 2 2 8 /tmp/psidtopgm-data.txt > /tmp/psidtopgm-result.pgm
$ printf 'exit status: %s\n' "$?"
exit status: 0

There is no progress display. Success means that the command produced PGM data on standard output and returned zero. The 8 says that each sample is eight bits deep, so the output greyscale range is 0 through 255.

Checkpoint

The command above changes no source file. It creates or truncates only the redirected destination, so use a destination in a working directory you control.

4. Verify the PGM before using it

PGM has a short text header followed by binary or plain pixel data. Inspect the header with a tool that does not try to display the whole file:

$ xxd -g1 -l 15 /tmp/psidtopgm-result.pgm
00000000: 50 35 0a 32 20 32 0a 32 35 35 0a 00 01 02 03  P5.2 2.255....
$ file /tmp/psidtopgm-result.pgm
/tmp/psidtopgm-result.pgm: Netpbm image data, size 2 x 2, rawbits, greymap

Look for the P5 magic number, the expected dimensions and a suitable maximum value. Do not use a text editor to inspect the pixel payload. The four bytes in this example are the samples 00, 01, 02 and 03; the output is not a resized or interpreted rendering of the PostScript page.

5. Use non-default dimensions carefully

Repeat the conversion with the actual image geometry from the PostScript image operator. For example, a 640 by 480 image with eight bits per sample would be:

$ psidtopgm 640 480 8 /path/to/extracted-image-data.txt > /path/to/image.pgm

The dimensions are not a request to resize anything. They tell the decoder how to group the incoming samples into rows. A wrong width can make every following row misaligned, while a wrong height can leave the output incomplete or cause an end-of-file error. Confirm the result with file and, where practical, a trusted image viewer.

The utility has no command-specific options. Its common Netpbm option handling does not replace the required positional values. Do not add guessed flags for width, height or scaling.

6. Handle failures without losing a good output

Typical failures are straightforward:

  • An input-open error means the filename is wrong or unreadable. Check it with ls -l and test -r.
  • A columns or rows error means a supplied dimension is not positive.
  • An end-of-file or read error usually means that the extracted stream is too short for the requested geometry, or that its sample depth does not match the source.
  • A valid PGM header does not prove that the image is visually correct. Geometry and extraction errors can still produce a file.

Shell redirection truncates an existing destination before psidtopgm starts. If the destination matters, write a temporary file and replace the old one only after verification:

$ psidtopgm 2 2 8 /tmp/psidtopgm-data.txt > /tmp/image.pgm.new
$ file /tmp/image.pgm.new
/tmp/image.pgm.new: Netpbm image data, size 2 x 2, rawbits, greymap
$ mv -- /tmp/image.pgm.new /tmp/image.pgm

Warning

mv replaces the destination if it already exists. Keep the original under a backup name if you need a rollback. No command here requires sudo; use elevated access only if your chosen input or output directory is deliberately restricted, and do not use it to compensate for incorrect image parameters.

Done means

  • You extracted only the hexadecimal image data, rather than feeding a complete PostScript file to this narrow tool.
  • The width, height and bits per sample match the PostScript image definition.
  • psidtopgm returned status 0 and produced a PGM stream.
  • The output header reports the expected dimensions and greyscale range.
  • The source data and any previously useful output remain recoverable until the replacement has been checked.