Home / Alt manpages / ppmtojpeg(1)

  • ppmtojpeg(1)
  • User command
  • linux

Replace ppmtojpeg Safely with pnmtojpeg

You will convert a PNM image to a JPEG file while dealing correctly with the obsolete ppmtojpeg name. On this machine, Netpbm 11.05.02 installs ppmtojpeg as a symbolic link to pnmtojpeg. Allow about ten minutes if the input file is ready. You need a readable PBM, PGM or PPM image and a writable destination directory. The conversion normally needs no elevated privileges.

1. Confirm the installed replacement

The ppmtojpeg manual page does not document a separate converter. It says that the program was replaced by pnmtojpeg in Netpbm 9.19, released in September 2001. Check the package and both command paths before putting the name into a script:

$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
$ command -v ppmtojpeg
/usr/bin/ppmtojpeg
$ readlink -f "$(command -v ppmtojpeg)"
/usr/bin/pnmtojpeg

Package versions vary by distribution. The useful check is that the legacy command resolves to the current pnmtojpeg implementation. For new scripts, call pnmtojpeg directly so the name describes the accepted input family.

2. Check the input and destination

pnmtojpeg accepts PBM, PGM or PPM input. It reads a named file, or standard input when you omit the filename, and writes a JFIF image to standard output. Inspect your source without changing it:

$ file /path/to/input.ppm
/path/to/input.ppm: Netpbm image data, size 640 x 480, rawbits, pixmap
$ test -r /path/to/input.ppm && echo readable
readable
$ test -d /path/to/output-directory && echo destination-ready
destination-ready

The output is a stream, so shell redirection decides which file receives it. Do not use sudo for an ordinary image in your own directory. If the input is in a protected directory, fix the access boundary deliberately or copy it to a working directory; running the converter as root can leave a root-owned output that you cannot edit later.

3. Convert to JPEG without destroying an existing file

Shell > truncates its destination before the converter starts. The following pattern refuses to replace an existing output, then writes a new file:

$ input=/path/to/input.ppm
$ output=/path/to/output-directory/image.jpg
$ test ! -e "$output" || { printf 'Refusing to overwrite: %s\n' "$output" >&2; exit 1; }
$ pnmtojpeg "$input" > "$output"
$ printf 'converter status: %s\n' "$?"
converter status: 0

Status 0 means the command completed successfully. It does not prove that the image has the dimensions or visual quality you expected, so verify the file before removing the original. If the command fails, inspect its diagnostic and check the destination rather than assuming a usable JPEG was produced.

There is no state to undo in Netpbm itself: the input is read, not modified. If the output command created a partial file after an error, remove that partial file only after confirming its path. Keep the source PNM until the JPEG has passed your checks.

4. Choose quality when the default is not suitable

The default quality is 75. The manual describes quality as a 0 to 100 scale: higher values generally preserve more detail and create larger files. Values around 50 to 95 are the normal working range; values above about 95 usually cost a lot of space for little visible benefit.

$ pnmtojpeg -quality=85 "$input" > "$output"
$ file "$output"
/path/to/output-directory/image.jpg: JPEG image data, JFIF standard 1.01, ...

The abbreviated option spelling is accepted by Netpbm, but use the full -quality=85 form in scripts. If you need a smaller file, try a lower value and compare the result. If edges or fine detail look poor, increase the value in steps of five or ten. JPEG is lossy, so do not use this route for data that must survive byte-for-byte or pixel-perfect reproduction.

5. Understand colour and greyscale output

For colour PPM input, the default output uses YCbCr, which usually compresses better than RGB. For PBM or PGM input, the default is greyscale. Select greyscale explicitly when converting a colour PPM that should become a monochrome JPEG:

$ pnmtojpeg -greyscale -quality=85 "$input" > /path/to/output-directory/mono.jpg
$ file /path/to/output-directory/mono.jpg
/path/to/output-directory/mono.jpg: JPEG image data, JFIF standard 1.01, ...

-grayscale and -greyscale are equivalent spellings. The -rgb option requests RGB output and fails if the input is not PPM. It is a format choice, not a quality setting. Leave it out unless a downstream consumer specifically needs RGB JPEG data.

6. Verify the result and handle failures

Use file as a quick format check. It should identify JPEG image data and report a JFIF file. For a stronger check, open the result with a trusted image viewer or pass it to an installed JPEG reader. A successful exit status alone cannot detect an input that was valid but not the image you intended.

$ test -s "$output" && echo non-empty
non-empty
$ file "$output"
/path/to/output-directory/image.jpg: JPEG image data, JFIF standard 1.01, ...

If the input cannot be opened, check the path and permissions. If the command rejects an option, consult man pnmtojpeg; the old ppmtojpeg page only explains the replacement relationship. If a pipeline supplies standard input, remember that the EXIF option cannot also read its EXIF data from standard input. Keep EXIF handling separate by naming an EXIF file.

To make a replacement safely, write to a new filename, verify it, then rename it over the old file only when that replacement is intentional. Make a backup first if the old JPEG is valuable. Renaming over a file changes state and may remove your easy recovery path, so do not include it in an unreviewed batch command.

Done means

  • You confirmed the installed Netpbm version and that ppmtojpeg resolves to pnmtojpeg.
  • The source is a readable PBM, PGM or PPM file and the destination is writable.
  • The conversion returned status 0 without overwriting an existing output.
  • You checked that the result is a non-empty JFIF JPEG and inspected it when appearance matters.
  • You chose quality and greyscale or RGB output for the consumer that will read the file.
  • The original PNM remains available until the JPEG has been verified.