Home / Alt manpages / pgmslice(1)

  • pgmslice(1)
  • User command
  • linux

Inspect a PGM Row or Column with pgmslice

pgmslice prints the sample values along one row or column of a PGM image, each labelled with its position. 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. Nothing here needs sudo, and nothing changes the image or system configuration.

1. Check which command you have

The installed pgmslice is an older interface kept around for compatibility. Its own manual page says it became obsolete in Netpbm 10.3 and points you at pamslice, but the binary here still handles the row and column selection used throughout this guide, so it is still worth knowing when an old script or runbook names it directly.

$ command -v pgmslice
/usr/bin/pgmslice
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1

Checkpoint

command -v prints nothing? Install Netpbm through your normal system administration process. Do not download a replacement binary into a temporary directory and put it ahead of the packaged one on 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, and lines starting with # are comments.

$ 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

Swap IMAGE.pgm for your own file's path. This read-only check earns its keep before you pick an index: rows count from the top, columns from the left, and both start at zero. In the example above, valid rows run 0 through 2 and valid columns 0 through 3.

Checkpoint

Make sure this really is the image you meant to inspect. A path typo or a similarly named fixture is an easy trap when several test images sit in the same directory.

3. Extract one horizontal row

Pass --row with the zero-based row number. pgmslice writes one line per pixel in that row, each line pairing the zero-based column number with the PGM sample value:

$ pgmslice --row=1 IMAGE.pgm
0 4
1 5
2 6
3 7

Row 1 in the sample image is the middle row, so its values are 4, 5, 6 and 7. This is data, not a picture: the first column is position, the second is the greyscale sample.

The long option also works with a space, as --row 1. Keep the index separate from the input path in scripts, and quote any path that might contain spaces:

$ pgmslice --row=0 -- 'directory with spaces/IMAGE.pgm'
0 0
1 1
2 2
3 3

4. Extract one vertical column

Swap to --column for a vertical slice. Now the first field is 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 is the third column. That gives values 2, 6 and 0 in the example above.
  • Expect no diagnostic text. A successful run stays quiet.
  • Never mix selections. Do not pass --row and --column together, and never treat an out-of-range index as a valid result.

Check the dimensions first, then pick an index smaller than the corresponding width or height.

5. Verify the result with the current replacement

For new scripts, reach for pamslice. It replaced pgmslice, keeps the same PGM behaviour and additionally accepts other Netpbm image types, making the row and column convention just as 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 whenever you are migrating a check: matching output is a practical way to confirm the old command was not quietly 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, though neither matters for a one-plane PGM.

6. Handle the common failures

Run pgmslice IMAGE.pgm with no selection and the installed command tells you to specify either -column or -row. Add exactly one 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 turns out to be a different Netpbm image type. Do not use sudo to paper over a wrong path or hand a script access it should not have.

Neither tool edits the input, so there is nothing to undo: recovery just means fixing the path or index and rerunning the read-only check. Redirecting output to a report? Pick a new report path, or confirm the destination first, since the shell can overwrite an existing file before the program even starts.

Done means

  • Version confirmed. The packaged Netpbm version and the executable in use are both known.
  • Dimensions checked. PGM dimensions were confirmed and zero-based row or column numbering was used.
  • Output read correctly. The first field is the position, the second is the sample value.
  • One direction only. A single selection was made and checked against the expected image line.
  • Modern tool preferred. New automation uses pamslice, with a tested compatibility path for old pgmslice calls.
  • Nothing else touched. No image, service, package or persistent configuration was changed.