The Independent JPEG Group's free implementation of JPEG (which is required for JPEG support in most any package, including ImageMagick) includes a program named "cjpeg" for doing exactly this. "djpeg" does the reverse.
The PerlMonk tr/// Advocate
Comment on Re: perl script to convert .ppm image to .jpeg image