Home / Alt manpages / ppmtorgb3(1)

  • ppmtorgb3(1)
  • User command
  • linux

Split a PPM into Red, Green and Blue Greymaps with ppmtorgb3

You will finish with three greymap files containing the red, green and blue channels extracted from one PPM image. The installed Netpbm 11.5.2 command writes them beside the input, using the suffixes .red, .grn and .blu.

Allow about ten minutes. You need Netpbm's ppmtorgb3, a readable PPM file, and write access to the directory holding it. The operation creates three files but does not edit the source image. No elevated privileges are normally required.

1. Check the installed command

Confirm the executable and package version before relying on a script. This is a read-only check:

$ command -v ppmtorgb3
/usr/bin/ppmtorgb3
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
$ ppmtorgb3 --version
ppmtorgb3: Using libnetpbm from Netpbm Version: Netpbm 11.5.2

The package revision can differ between distributions. The useful part of this check is that the command is the installed Netpbm program, rather than a similarly named wrapper or a script from another directory. The command has no ppmtorgb3-specific options. It accepts an optional input filename and also recognises common libnetpbm options.

Checkpoint

If command -v finds nothing, stop here and install Netpbm through your normal package-management process. Do not copy a binary into a system directory just to make this one example work.

2. Inspect the input and choose a destination

Use a working copy when testing an unfamiliar image. This keeps the original available if you later need to compare channels or repeat the split:

$ mkdir -p /tmp/ppm-channels
$ cp --preserve=all /path/to/input.ppm /tmp/ppm-channels/input.ppm
$ file /tmp/ppm-channels/input.ppm
/tmp/ppm-channels/input.ppm: Netpbm image data, size = 640 x 480, rawbits, pixmap

Replace /path/to/input.ppm with an actual path. The output files are created in the current directory, not necessarily beside a file named with an absolute path. Changing directory first makes the destination explicit:

$ cd /tmp/ppm-channels
$ test -r input.ppm && echo 'input is readable'
input is readable

Do not use sudo for this conversion. If the destination is a protected directory, choose a directory you own or fix its permissions through the normal administrative process rather than running an image converter as root.

3. Split a named PPM

Run the command with the input filename:

$ ppmtorgb3 input.ppm
$ printf 'exit status: %s\n' "$?"
exit status: 0

For input.ppm, ppmtorgb3 removes the input extension and appends the three channel suffixes. Expect these names:

$ ls -l input.red input.grn input.blu
-rw-r--r-- 1 you you 307215 Sep 26 12:00 input.blu
-rw-r--r-- 1 you you 307215 Sep 26 12:00 input.grn
-rw-r--r-- 1 you you 307215 Sep 26 12:00 input.red

File sizes and timestamps vary. The important result is three non-empty files with the same dimensions as the source. The command writes each output as a PGM greymap, using the red, green or blue intensity at each pixel.

Checkpoint

Verify the files as image data, not only as directory entries:

$ file input.red input.grn input.blu
input.red: Netpbm image data, size = 640 x 480, rawbits, greymap
input.grn: Netpbm image data, size = 640 x 480, rawbits, greymap
input.blu: Netpbm image data, size = 640 x 480, rawbits, greymap
$ test -s input.red && test -s input.grn && test -s input.blu && echo 'all channels are non-empty'
all channels are non-empty

4. Protect existing channel files

ppmtorgb3 chooses its output names for you. If those names already exist, do not assume that a second run is harmless: an output conversion can replace a file you wanted to keep. Check first:

$ for f in input.red input.grn input.blu; do
>     if test -e "$f"; then printf 'already exists: %s\n' "$f"; fi
> done
already exists: input.red

If the files are valuable, make a dated or otherwise unambiguous backup before running the converter again:

$ mkdir -p backup-before-split
$ cp --preserve=all input.red input.grn input.blu backup-before-split/
$ ppmtorgb3 input.ppm

The copy is your recovery path if the new outputs are not what you expected. Removing an old output or backup is irreversible, so leave cleanup until you have inspected the replacement. If you need an atomic workflow for a larger job, run each input in its own temporary directory and move the checked outputs into place afterwards.

5. Split data from standard input

Omit the filename when the PPM is arriving on standard input. ppmtorgb3 then uses the fixed base name noname:

$ cat input.ppm | ppmtorgb3
$ ls -l noname.red noname.grn noname.blu
-rw-r--r-- 1 you you 307215 Sep 26 12:00 noname.blu
-rw-r--r-- 1 you you 307215 Sep 26 12:00 noname.grn
-rw-r--r-- 1 you you 307215 Sep 26 12:00 noname.red

That fixed name is easy to overlook in a pipeline. Run it in a clean temporary directory or check for existing noname.* files before starting. If a pipeline creates several images, rename or move each completed set immediately so one run cannot be confused with the next.

Prefer input redirection when you want the source to be obvious in a short command:

$ ppmtorgb3 < input.ppm
$ file noname.red noname.grn noname.blu
noname.red: Netpbm image data, size = 640 x 480, rawbits, greymap

6. Diagnose a failed split

A missing or unreadable input is reported on standard error and produces a non-zero status:

$ ppmtorgb3 missing.ppm
ppmtorgb3: Unable to open file 'missing.ppm' for reading.  fopen() returns errno 2 (No such file or directory)
$ printf 'exit status: %s\n' "$?"
exit status: 1

The exact diagnostic can vary with the operating system. Check the path and permissions without changing anything:

$ ls -l /path/to/input.ppm
$ test -r /path/to/input.ppm && echo readable || echo 'not readable'
$ file /path/to/input.ppm

If the input is not a valid PPM, the converter can fail while reading it. Preserve the source, inspect its header with a suitable image tool, and do not treat the existence of partial outputs as success. Rerun in a clean directory after correcting the input. A successful exit status and three valid greymaps are the useful completion checks.

Done means

  • The installed Netpbm version and executable were checked.
  • The source PPM remains untouched and the destination is writable.
  • Named input produced .red, .grn and .blu PGM files.
  • Each output is non-empty and has the expected dimensions.
  • Existing outputs were checked or backed up before rerunning.
  • Standard-input runs were isolated from stale noname.* files.