jpeg2eps

Convert JPEG to EPS, retaining compression

Despite JPEG being a nearly ubiquitous image format, it is often necessary to convert JPEG files to encapsulated PostScript (EPS) for inclusion in other documents (eg LaTeX). Unfortunately converting JPEG to EPS typically produces much larger files, since the compressed JPEG data is usually decompressed before being written to the EPS file. Since JPEG compression saves about 90% of the space, you typically get EPS files that are over ten times larger than the original JPEG (actually it’s more like 20 times, since the uncompressed binary data is usually written as hexadecimal ASCII text).

It turns out there is actually no need to decompress, since PostScript level 2 and higher can decompress JPEG internally - thus it is possible to “wrap” the compressed JPEG data in some PostScript to make a valid EPS file which is only about 25% bigger than the original JPEG.

This can be done with the utility jpeg2ps, which is available on several platforms.

Alternatively, if you have GhostScript installed on your machine you may prefer the utility jpeg2eps supplied here. This does much the same thing but is written in PostScript, so it can be run on any platform that supports GhostScript. The output EPS is standards-compliant so it should work fine just about anywhere.

Obtaining jpeg2eps

Download jpeg2eps from here, or in one lump as a gzipped tarfile. The current versions were created on 3rd August 2003. The included files are:

Installation

in unix:
  1. Put the above files in a directory in your search path (if you downloaded the gzipped tarfile, do gunzip jpeg2eps.tar.gz; tar xvf jpeg2eps.tar; rm -i jpeg2eps.tar then move the files in jpeg2eps_source to a directory in your search path).
  2. Change to that directory and do chmod +x jpeg2eps jpeg2eps.ps viewjpeg.ps
  3. To improve speed, it's best to edit jpeg2eps and hard-code the absolute paths for jpeg2eps.ps and viewjpeg.ps in the variables jpeg2epspath and viewjpegpath, respectively, and comment out the two lines that use which.
  4. Do source ~/.cshrc
in another OS:

Usage

in unix: in another OS:
  1. Put the JPEG files in the same directory as jpeg2eps.
  2. Open dojpeg2eps.ps in a text editor and add the names of the JPEG files following the instructions in that file. Save dojpeg2eps.ps as plain text.
  3. Make sure GhostScript/GhostView is not in “safer” mode.
  4. Open dojpeg2eps.ps with GhostScript/GhostView. WARNING: this will produce EPS files with names that match the input JPEG files but have any trailing “.jpg” or “.jpeg” removed and “.eps” appended. If files with these names already exist they will be OVERWRITTEN, no questions asked.
  5. The resulting EPS files should open in GhostScript/GhostView without errors (but it’s a good idea to check - this will be done automatically if showoutput is true in dojpeg2eps.ps; each EPS file appears on a separate page).

Limitations / quirks / troubleshooting

  1. First up, check that you have the latest versions.
  2. “invalidaccess”, “invalidfileaccess”, “ioerror”, “undefinedfilename” errors: these usually arise from running GhostScript/GhostView in “safer” or “paranoid safer” mode, which prohibits file writing and reading. So don’t use the -dSAFER or -dPARANOIDSAFER flags; you may need to use -dNOSAFER (these settings might be in the “preferences” menu item). Also check that you've got the filenames right. The script jpeg2eps handles these details automatically.
  3. The script jpeg2eps does not put \ ahead of ( or ) in filenames, so filenames with unbalanced parentheses will choke GhostScript.
  4. Only JPEG baseline, extended sequential, and progressive files are supported. Occasionally the conversion proceeds without reporting an error, but the resulting EPS file doesn't work. If you can't convert a particular JPEG, try opening it in an image editor and re-saving.
  5. Some versions of GhostView will only display the part of the EPS file that fits on the selected page size. Don’t be alarmed - the full image is there (it’s just clipped by the viewer), and will be shown properly if the EPS is included in another document (eg. LaTeX).
  6. No preview TIFF/PICT/WMF raster image is generated. So you may get a blank box when including the EPS in your document, but the correct image should appear when the document is printed or converted to a PDF.
  7. The EPS files can only be opened/printed on devices which understand level 2 PostScript or better (that’s just about everything these days). Transparency requires level 3.

Tips and tricks

If you open the resulting EPS files in a text editor, it’s easy to alter the brightness/contrast of the images or make some colors transparent.

Copyright / distribution / disclaimer

jpeg2eps, jpeg2eps.ps and dojpeg2eps.ps are copyright © Andrew Kiss 2003. They are free software; you can redistribute and/or modify them under the terms of the GNU General Public License (please email me any bug fixes, scripts or other improvements and I’ll put them on this web page). They are distributed in the hope that they will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

PostScript is a trademark of Adobe Systems Inc.

Page last modified: 2nd September 2003.