Home / Alt manpages / pnmtojpeg(1)

  • pnmtojpeg(1)
  • User command
  • linux

Convert PNM Images to JPEG Safely with pnmtojpeg

pnmtojpeg turns a PBM, PGM or PPM image into a JPEG, and by default it throws away detail you cannot get back. The file it produces is technically a JFIF file, the container almost everyone just calls a JPEG. The command writes binary image data to standard output, so you choose the destination with shell redirection. Allow about ten minutes for a first conversion, plus time to inspect the result, and keep the original: JPEG compression is lossy.

1. Check the installed command

This guide follows Netpbm 11.5.2 from Debian package netpbm 2:11.05.02-1.1build1. The installed manual page is dated 23 April 2007, while the command reports its library version separately. Check your machine before relying on version-specific details:

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

The version output includes build details after the library version, so the abbreviated result above is only a guide to what you should see. A missing command means the Netpbm package is not installed or is not on your PATH. Installing packages is a separate system-management action and normally needs elevated privileges; conversion itself does not.

Checkpoint: prepare a new destination

Before running the converter, decide where the new JPEG will go. Do not redirect straight over a valuable existing file: the shell opens a destination with > before pnmtojpeg starts, so a failed conversion can still truncate the old file.

$ test -r /path/to/input.ppm && echo 'input is readable'
input is readable
$ test ! -e /path/to/output.jpg && echo 'destination is new'
destination is new

Replace the placeholder paths with real paths. These checks only read directory metadata and do not require sudo.

2. Convert a PPM image at the normal quality

Pass the PNM filename as the final argument and redirect standard output to a new JPEG:

$ pnmtojpeg -quality=75 /path/to/input.ppm > /path/to/output.jpg
  • Quality ranges from 0 to 100 and defaults to 75. Higher values usually make a larger file with fewer visible compression defects.
  • 50 to 95 is the documented range for reasonable results, and 75 is a sound first attempt to compare visually against.
  • Above about 95 usually costs much more space for little visible improvement.
  • Below about 25 can create files that some JPEG readers cannot decode.

Verify both the exit status and the file type. Run the status check immediately after the conversion, before another command changes $?:

$ printf '%s\n' "$?"
0
$ file /path/to/output.jpg
/path/to/output.jpg: JPEG image data, JFIF standard 1.01, ...

Your file description may differ between versions. It should identify a JPEG or JFIF image, not an empty file or plain text. A zero exit status confirms that the conversion completed, but it does not prove that the chosen quality is visually suitable.

3. Choose the output colour mode deliberately

With a PPM input and no colour option, the output uses YCbCr, which normally compresses better than RGB. PBM and PGM inputs produce grayscale output by default. Use -grayscale or its British spelling alias -greyscale when a colour PPM should become a one-component grayscale JPEG:

$ pnmtojpeg -grayscale -quality=85 /path/to/colour.ppm > /path/to/grey.jpg
$ file /path/to/grey.jpg
/path/to/grey.jpg: JPEG image data, JFIF standard 1.01, ... components 1

Use -rgb only when you specifically need RGB JPEG data. The manual says it fails for non-PPM input, and RGB output is commonly larger. This option is useful when matching a workflow that expects the PPM colour space, not as a general quality improvement.

4. Add metadata without changing pixels

-density records resolution information in the JFIF header. It does not resize or resample the image:

$ pnmtojpeg -quality=85 -density=300x300dpi \
    /path/to/input.ppm > /path/to/print.jpg

The default is 1x1 with unspecified units. Horizontal and vertical values can be from 1 to 65535. If you set unequal densities, readers may display the pixels with a changed aspect ratio even though the raster was copied pixel-for-pixel. Use unequal values only when they describe the source correctly.

For a final file, -optimize can reduce size by optimising entropy coding. It can run more slowly and use substantially more memory, without changing image quality or decompression speed:

$ pnmtojpeg -quality=85 -optimize \
    /path/to/input.ppm > /path/to/final.jpg

If a large conversion needs a memory ceiling, -maxmemory accepts thousands of bytes, or millions when followed by M. The command can use temporary files when it needs more space. Do not confuse this with a guarantee that the whole operation fits within the chosen limit.

5. Handle pipelines and progressive output carefully

With no input filename, pnmtojpeg reads standard input. That makes it useful at the end of a Netpbm pipeline:

$ bmptoppm /path/to/input.bmp | pnmtojpeg -quality=85 > /path/to/output.jpg

Warning

Do not use standard input for both the image and -exif=-. The EXIF option expects a specially structured binary file whose first two bytes give its length, so it is not a place to pass an ordinary text note. If you need an ordinary JPEG comment, use -comment='text here', but remember that comments become part of the output file and may disclose information.

-progressive writes a progressive JPEG that can display successive scans during transfer. The manual warns that some decoders cannot display this form. Use it only when your readers and delivery path support it, and verify the result with the software that will consume it. For unreliable transmission, -restart=1 adds restart markers and can limit damage after a transmission error, at the cost of a little extra space.

6. Avoid repeated JPEG loss

JPEG discards information during compression. Repeatedly converting a JPEG to PNM, editing it, and converting it back accumulates loss, especially in drawings, charts and sharp-edged artwork. Keep an original or a lossless working format, perform edits there, and make the JPEG the last export. If the image is a drawing and artefacts are visible, try a different quality value and, where appropriate, a moderate -smooth value such as 10. Smoothing can reduce dithering noise, but excessive smoothing visibly blurs detail.

Recovery

There is no undo command for a conversion that has overwritten an old file. If you made an unsafe redirection, stop and check whether a backup, filesystem snapshot or versioned copy exists. For future conversions, write to a new name, inspect it, and only then replace the old file through your normal backup procedure.

Done means

  • Library confirmed: pnmtojpeg --version identified the installed Netpbm library.
  • Original intact: the source PNM remains intact and the destination was not blindly overwritten.
  • Output checked: the command returned status 0 and file identifies a JPEG or JFIF image.
  • Settings deliberate: quality, colour mode, density and progressive output were chosen for the real consumer.
  • Fallback kept: the original or a lossless working copy is retained for future edits.