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
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.
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:
- jpeg2eps: unix shell script (v. 1.00) that calls jpeg2eps.ps
with the files you specify
- dojpeg2eps.ps: example PostScript code (v. 1.06) showing how to use
jpeg2eps.ps if you don't use unix
- jpeg2eps.ps: the PostScript code that does the conversion (v. 1.20)
- viewjpeg.ps: a PostScript utility used by jpeg2eps.ps
(viewjpeg.ps is copyright © 1994 Thomas Merz, and part of the
in another OS:
- 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).
- Change to that directory and do chmod +x jpeg2eps jpeg2eps.ps
- 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.
- Do source ~/.cshrc
- put the above files in a single directory
in another OS:
- In any directory you just type jpeg2eps file1 file2 file3 ...
or jpeg2eps *.jpg etc ... easy! 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. jpeg2eps also uses a temporary file
“dojpeg2eps-temp.ps”. If files with these names already exist
they will be OVERWRITTEN, no questions asked.
- jpeg2eps has optional flags (default is silent with no checking or
- -v: verbose
- -c: checking: parse each output EPS with gs
- -d: display each output EPS (implies -c)
- Put the JPEG files in the same directory as jpeg2eps.
- 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.
- Make sure GhostScript/GhostView is not in “safer” mode.
- 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.
- 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
- First up, check that you have the latest
“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.
- The script jpeg2eps does not put \ ahead of ( or ) in filenames, so
filenames with unbalanced parentheses will choke GhostScript.
- 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.
- 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).
- 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.
- 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.
- The raw data consists of an 8-bit number (0 to 255) for each color
component for each pixel. Brightness and contrast are controlled by
/Decode, which has a pair of values for each color component (eg. two
numbers for each of red, green and blue) specifying the intensity (0: none,
1: max) that raw data values 0 and 255 correspond to. All intermediate raw
data values will be linearly mapped between these two intensities, and
intensities outside the range 0 to 1 will be clipped to 0 or 1. eg for RGB
/Decode [ 0.5 1.5 0.5 1.5 0.5 1.5 ] % lighten all colors by
/Decode [ -0.2 0.8 -0.2 0.8 -0.2 0.8 ] % darken all colors by 20%
/Decode [ 0 1.5 0 1.5 0 1.5 ] % lighten highlights by 50%
/Decode [ -0.2 1 -0.2 1 -0.2 1 ] % darken shadows by 20%
/Decode [ -0.5 1.5 -0.5 1.5 -0.5 1.5 ] % increase contrast (pivot about
/Decode [ 1 0 1 0 1 0 ] % invert all colors
/Decode [ -0.5 1.5 0 1 0 1 ] % increase contrast of red
- If your printer understands PostScript level 3 you can make some color
ranges transparent by setting /ImageType 4 (rather than 1) and
specifying in /MaskColor the range of each color component you don't want
painted (so the background remains visible wherever all color
components fall within the specified ranges). The values are in the range 0
to 255 (the raw data is used, before mapping by Decode). JPEG compression
adds some variation at the edges of flat colors, so you need to specify a
range of colors which encompasses this. eg for RGB colors:
/MaskColor [ 0 50 0 50 0 50 ] % don't paint dark greys and
/MaskColor [ 245 255 245 255 245 255 ] % don't paint white and
/MaskColor [ 118 138 118 138 118 138 ] % don't paint mid-greys
/MaskColor [ 0 50 205 255 0 50 ] % don't paint strong greens
- Masking and contrast adjustment can be combined to give seamless
cropping of the background. Eg with RGB colors the following will make the
masking threshold appear white, so it will blend seamlessly with a white
/MaskColor [ 245 255 2 copy 2 copy ] % don't paint white and
/Decode [ 0 255 245 div 2 copy 2 copy ] % lighten highlights so masking
threshold appears white
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
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
PostScript is a trademark of Adobe Systems Inc.
Page last modified: 2nd September 2003.