Home / Alt manpages / pamslice(1)

  • pamslice(1)
  • User command
  • linux

Read a Netpbm Image Row or Column with pamslice

You will extract a single horizontal row or vertical column from a Netpbm image and read its sample values as a table. This is useful for checking a border, measuring a scan line, comparing colour channels or sending one cross-section to a plotting tool. Allow about ten minutes if the image path and slice coordinates are already known. No elevated privileges are needed for an image you can read and a directory you can write.

1. Check the installed command

This guide uses the Netpbm command installed on this machine. The package is Netpbm 11.5.2, from Debian package netpbm 2:11.05.02-1.1build1. The manual page is dated 8 February 2010, so the examples describe the installed behaviour rather than promising identical output from every release.

$ command -v pamslice
/usr/bin/pamslice
$ pamslice --version
pamslice: Using libnetpbm from Netpbm Version: Netpbm 11.5.2

The version command prints several build details on this installation. The useful checkpoint is the Netpbm version line and the path returned by command -v. If your command comes from another installation, check its own manual page before putting it into a script.

2. Confirm the image dimensions

pamslice numbers rows from the top and columns from the left, starting at zero. Before choosing a coordinate, inspect the image dimensions with an installed Netpbm utility such as pamfile:

$ pamfile /path/to/image.ppm
/path/to/image.ppm: PPM raw, 640 by 480 pixels

The exact description varies with the file format. For a 640 by 480 image, valid rows are 0 through 479 and valid columns are 0 through 639. Do not use a human counting convention here: row 1 is the second row, not the first.

If pamfile is not installed, use a format-aware tool already present on your system rather than guessing from the file name. A wrong coordinate is rejected when it is outside the image, but an in-range coordinate can still be the wrong part of the picture.

3. Extract one row

Pass -row with the zero-based row number. The input filename is optional; when omitted, pamslice reads the image from standard input.

$ pamslice -row=120 /path/to/image.ppm
0 18 22 31
1 19 23 32
2 20 24 33

Each output line represents one pixel in the selected row. The first number is the column number. The remaining numbers are the sample values, in plane order. The output above is abbreviated: a real row continues until the final column.

For a PBM or PGM image there is one plane. For a PPM image, plane 0 is red, plane 1 is green and plane 2 is blue. Without -plane, all planes are printed on each line.

Checkpoint: confirm the first output number starts at zero and that the number of output lines matches the image width. Save the result only when you need it later:

$ pamslice -row=120 /path/to/image.ppm > /tmp/row-120.txt
$ wc -l /tmp/row-120.txt
640 /tmp/row-120.txt

4. Extract one column or one colour plane

Use -column for a vertical slice. The first number is then the row number, and the output has one line per image row.

$ pamslice -column=300 /path/to/image.ppm
0 44 51 60
1 45 52 61
2 47 54 63

To print only one PPM channel, add -plane. Plane numbers also start at zero:

$ pamslice -column=300 -plane=1 /path/to/image.ppm
0 51
1 52
2 54

The second column is now the selected sample value rather than a complete RGB tuple. Plane 1 means green for PPM. Do not assume the same meaning for every Netpbm image type; the manual defines the PPM mapping, while other formats have their own planes.

You cannot specify -row and -column together. If you do, the installed command stops with an error such as You cannot specify both -row and -col. Choose one direction and rerun it.

5. Plot a slice when xmgr is available

The -xmgr option adds headers for an xmgr-compatible plot. It does not change the selected row or column and it does not create an image file.

$ pamslice -row=0 -plane=0 -xmgr /path/to/image.ppm > /tmp/top-red.xmgr
$ sed -n '1,6p' /tmp/top-red.xmgr
@    title "Graylevel"
@    subtitle "from (0,0) to (640,0)"
@    xaxis label "column"
@    yaxis label "grayvalue"
@    type  nxy
0 18

The headers and coordinate range are generated from the selected slice. The wording says "grayvalue" even when you select a colour plane, so treat the values as samples from that plane rather than as a greyscale conversion. If your plotting program does not understand xmgr input, keep the ordinary two-column or multi-column output and adapt it separately.

6. Handle failures without changing the source image

pamslice reads the source and writes the table to standard output. It does not modify the image. The main overwrite risk is shell redirection: > truncates an existing destination before pamslice starts.

For a valuable result, write to a new temporary name, check the command status, then rename it:

$ pamslice -row=120 /path/to/image.ppm > /tmp/row-120.txt.new
$ status=$?
$ if [ "$status" -eq 0 ]; then mv /tmp/row-120.txt.new /path/to/row-120.txt; else rm -f /tmp/row-120.txt.new; fi
$ printf 'pamslice status: %s\n' "$status"
pamslice status: 0

This changes only the destination table. If the command reports that the row or column is outside the image, inspect the dimensions and rerun with a valid zero-based coordinate. If it cannot open the input, check the path and read permission:

$ test -r /path/to/image.ppm && echo readable
readable

Do not add sudo to fix a coordinate error. Use elevated privileges only when your normal account genuinely lacks permission to read the source or write the chosen destination, and prefer copying the data into a directory you own when policy allows.

Done means

  • You confirmed which pamslice and Netpbm version will run.
  • You checked the image dimensions and used a zero-based row or column.
  • You can explain that output starts with a coordinate, followed by sample values in plane order.
  • You used -plane only when you needed one channel, and know that PPM plane 0, 1 and 2 are red, green and blue.
  • You checked the exit status and avoided overwriting a useful output table.