Read Netpbm Pixels as a Table with pamtable
You will inspect a Netpbm image as rows of numerical sample values, choose decimal, tuple or hexadecimal output, and feed an image through standard input when that is more convenient. The examples use Netpbm 11.5.2 from the Debian netpbm package. Allow about ten minutes if you already have a small PPM, PGM or PAM file ready.
The route
Jump straight to the step you need, or tick off Done means at the end.
This is a read-only workflow. pamtable does not alter the input image, and none of the commands below needs sudo. You need a shell, the netpbm package and a readable Netpbm image.
1. Check the installed command
Confirm which executable will run, then record the library version reported by the program:
$ command -v pamtable
/usr/bin/pamtable
$ pamtable --version
pamtable: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
...
The version text contains build details that vary by distribution. The useful checkpoint is the Netpbm version. This guide describes the command installed here, while the manual page identifies -tuple and -hex as features added in Netpbm 10.11.00.
2. Print the image in the default format
Pass one input filename. Replace the placeholder with an existing Netpbm image:
$ pamtable /path/to/image.ppm
For a three-pixel-wide, two-row RGB image with maximum sample value 255, the output has one line per image row and vertical bars between RGB tuples:
0 0 0|255 0 0| 0 255 0
255 255 255|128 64 32| 1 2 3
Samples inside one tuple are separated by spaces. Values are printed as decimal numbers, right-justified to the width needed for the image's maximum value. A bar therefore marks the boundary between pixels in a multi-sample image. A one-sample image, such as a greyscale raster, uses spaces between adjacent samples instead.
Checkpoint
The number of output lines should match the image height. If the rows are unexpectedly long, use pamfile to inspect the image dimensions and tuple type before changing the input.
3. Use tuple notation for RGB-style data
Add -tuple when each pixel should be visually grouped as a mathematical tuple:
$ pamtable -tuple /path/to/image.ppm
(0,0,0) (255,0,0) (0,255,0)
(255,255,255) (128,64,32) (1,2,3)
This format removes the vertical-bar separators and writes each tuple in parentheses. It is useful when copying a small raster into notes or comparing complete pixel values. It does not resize, colour-convert or otherwise interpret the samples; the values still come directly from the Netpbm image.
4. Use hexadecimal values
Use -hex when hexadecimal is easier to compare with byte-oriented data:
$ pamtable -hex /path/to/image.ppm
00 00 00|ff 00 00|00 ff 00
ff ff ff|80 40 20|01 02 03
The local command prints lower-case hexadecimal sample values and retains the tuple separators used by the normal layout. Do not combine -hex with -tuple. The manual marks that combination as invalid, so choose one representation for each run.
Hexadecimal is a display choice, not a conversion of the source file. In particular, a sample value of 1 appears as 01 for an image whose maximum value is 255. If the image uses a different maximum value, interpret the numbers using that image metadata rather than assuming eight-bit channels.
5. Read from standard input
The filename is optional. With no filename, pamtable reads the Netpbm image from standard input, so it can sit at the end of a pipeline:
$ cat /path/to/image.ppm | pamtable -tuple
(0,0,0) (255,0,0) (0,255,0)
(255,255,255) (128,64,32) (1,2,3)
For a file, the direct form is clearer and avoids an unnecessary cat process:
$ pamtable < /path/to/image.ppm
Keep the output on the terminal or redirect it to a new text file. Do not redirect it over the source image. Although the command only reads its input, shell redirection can truncate a destination before pamtable starts. If you need a saved table, use a distinct name:
$ pamtable /path/to/image.ppm > image-samples.txt
$ sed -n '1,5p' image-samples.txt
If you accidentally chose an existing destination, stop and check it before running another command. Recovery depends on whether the file was backed up or can be restored from version control or another copy; pamtable has no undo operation for shell redirection.
6. Narrow the data before printing
pamtable prints the complete raster. For a large image, the terminal output can become unwieldy, and a long line can be difficult to inspect. Filter the image first when you need only a region or selected channels:
$ pamcut -left 0 -right 9 -top 0 -bottom 9 /path/to/image.ppm | pamtable
$ pamchannel 0 /path/to/image.ppm | pamtable
These examples use other Netpbm commands and their options, so check their own manual pages against your installed release. The key boundary is that pamtable displays the samples it receives; it does not select rows, columns or planes itself.
For browsing a very wide table, pipe the result to less --chop-long-lines. If the output should be denser, the manual suggests reducing the image maximum value with pamdepth first. That changes the image data, so keep the original and write any transformed image to a new file.
7. Diagnose common mistakes
- "Cannot open" or a missing-file error: check the path and permissions with
ls -l /path/to/image.ppm. Do not addsudountil you know the file really requires elevated access. - Output appears to have the wrong shape: inspect the input with
pamfile /path/to/image.ppm. The program prints the raster's actual rows and tuples; it does not infer dimensions from a filename. - Hexadecimal and tuple modes were mixed: run one command with either
-hexor-tuple, not both. - The table is hard to read: use a smaller region, a pager, or a separate output file. Avoid opening a large generated table in a graphical editor that may alter line endings or whitespace.
When a command fails, the input image is normally unchanged because pamtable only reads it. Check the exit status immediately after the command with printf '%s\n' "$?"; a zero status means the command completed, not that you have visually confirmed every value.
Done means
command -v pamtablepoints to the intended Netpbm installation.- The output has one line for each input row and the expected sample grouping.
- You selected decimal,
-tupleor-hexdeliberately, without combining the latter two. - Any redirected table uses a new destination, leaving the source image intact.