Inspect a PGM Row or Column with pgmslice
You will finish with a small, repeatable way to print the sample values along one row or column of a PGM image. The output includes the position of each value, so it is useful for checking a generated image, a test fixture, or a single suspicious line of pixels.
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 PGM file, and the Netpbm tools. The examples use netpbm version 2:11.05.02-1.1build1 as installed on this machine. No command in this guide needs sudo, and none changes the image or system configuration.
1. Check which command you have
The installed pgmslice is an older interface kept for compatibility. Its manual page says that it became obsolete in Netpbm 10.3 and recommends pamslice. The installed binary still accepts the row and column selection used below, so it remains useful when a script or an old runbook already names pgmslice.
$ command -v pgmslice
/usr/bin/pgmslice
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
Checkpoint: if command -v prints nothing, install the Netpbm package through your normal system administration process. Do not download a replacement binary into a temporary directory and put it ahead of the packaged command in PATH.
2. Confirm the PGM dimensions
PGM is a greyscale Netpbm format. Its header gives the width, height and maximum sample value. A plain-text PGM starts with P2, followed by those fields and then the samples. Comments beginning with # may appear in the header.
$ sed -n '1,8p' IMAGE.pgm
P2
# generated test image
4 3
9
0 1 2 3
4 5 6 7
8 9 0 1
Replace IMAGE.pgm with a path to your own file. This read-only check is worth doing before choosing an index: rows run from the top, columns from the left, and both start at zero. In the example, valid rows are 0 through 2 and valid columns are 0 through 3.
Checkpoint: make sure the file is the image you intended to inspect. A path typo or a similarly named fixture is an easy distraction when several test images sit in one directory.
3. Extract one horizontal row
Pass --row with the zero-based row number. The command writes one line for each pixel in that row. Each line contains the zero-based column number followed by the PGM sample value.
$ pgmslice --row=1 IMAGE.pgm
0 4
1 5
2 6
3 7
For the sample image, row 1 is the middle row, so its values are 4, 5, 6 and 7. The output is data, not a plotted image: the first column is the position and the second is the greyscale sample.
The long option can also be written with a space, as --row 1. Keep the index separate from the input path when writing scripts, and quote paths that may contain spaces:
$ pgmslice --row=0 -- 'directory with spaces/IMAGE.pgm'
0 0
1 1
2 2
3 3
4. Extract one vertical column
Use --column for a vertical slice. The first output field is now the zero-based row number, followed by the sample from that row.
$ pgmslice --column=2 IMAGE.pgm
0 2
1 6
2 0
Column 2 in the example is the third column, so the values are 2, 6 and 0. A successful command normally produces no diagnostic text. If you need to feed the values to another tool, remember that the position and sample are separate whitespace-delimited fields.
Do not specify both --row and --column. Do not silently treat an index outside the image as a useful result. Check the dimensions first, then choose an index less than the corresponding width or height.
5. Verify the result with the current replacement
For new scripts, use pamslice. It replaced pgmslice and retains the PGM behaviour while also accepting other Netpbm image types. Its output makes the same row and column convention explicit:
$ pamslice --row=1 IMAGE.pgm
0 4
1 5
2 6
3 7
$ pamslice --column=2 IMAGE.pgm
0 2
1 6
2 0
Run the replacement on the same file when migrating a check. Matching output is a practical verification that the old command was not being relied on for some undocumented wrapper behaviour. pamslice also supports --plane for selecting one plane of a multi-plane image and --xmgr for plot-oriented output. Neither is needed for a one-plane PGM.
6. Handle the common failures
If you run pgmslice IMAGE.pgm without a selection, the installed command reports that you must specify either -column or -row. Add exactly one selection and retry:
$ pgmslice IMAGE.pgm
pgmslice: You must specify either -column or -row
$ pgmslice --row=1 IMAGE.pgm
0 4
1 5
2 6
3 7
An unreadable, malformed, or unsupported input is an input problem, not a reason to add elevated privileges. Check the path and permissions with ordinary read-only commands, then try pamslice if the file is a different Netpbm image type. Do not use sudo to mask a wrong path or to grant a script access it should not have.
Neither tool edits the input. There is therefore no undo operation: recovery means correcting the path or index and running the read-only check again. If you redirect output to a report, choose a new report path or confirm the destination before using shell redirection, because the shell can overwrite an existing file before the program starts.
Done means
- You confirmed the packaged Netpbm version and the executable in use.
- You checked the PGM dimensions and used zero-based row or column numbering.
- You read the first output field as the position and the second as the sample value.
- You selected one direction only and checked the command's output against the expected image line.
- New automation uses
pamslice, while olderpgmslicecalls have a tested compatibility path. - No image, service, package or persistent configuration was changed.