Pillow
| Language | Python |
|---|---|
| License | MIT-CMU (HPND) |
| Repository | github.com/python-pillow/Pillow |
| Documentation | EPS format notes |
| Depends on | the Ghostscript executable (gs, gswin64c) for reading |
| Read / write | yes (rasterized by Ghostscript) / yes (raster images in L, RGB or CMYK) |
Image.open("file.eps") parses the header and the %%BoundingBox without Ghostscript, so im.size is available right away (in points, 1 point = 1 pixel at scale 1). It handles files that start with %!PS and files with the binary C5 D0 D3 C6 preview header. Loading the pixels runs Ghostscript.
When saving, Pillow writes the pixels as an EPS image; it doesn’t create vector art. Pillow reads only the first page of a multi-page file.
Installation#
Shell
pip install pillow
sudo apt install ghostscript # or: brew install ghostscript
On Windows install Ghostscript; Pillow finds gswin64c on PATH.
Basic usage: convert EPS to PNG#
Python
from PIL import Image
with Image.open("logo.eps") as im:
print(im.size) # bounding box size in points, e.g. (200, 100)
im.load(scale=4) # rasterize at 4 x 72 = 288 dpi
im.save("logo.png")
How to convert EPS to PNG with a transparent background#
Python
from PIL import Image
with Image.open("logo.eps") as im:
im.load(scale=4, transparency=True) # RGBA
im.save("logo.png")
How to convert EPS to an image of a given width#
Python
from PIL import Image
target_width = 1200
with Image.open("logo.eps") as im:
scale = max(1, -(-target_width // im.size[0])) # integer scale, rounded up
im.load(scale=scale)
im = im.resize((target_width, round(im.height * target_width / im.width)), Image.LANCZOS)
im.save("logo-1200.png")
How to save an image as EPS#
Python
from PIL import Image
with Image.open("photo.jpg") as im:
im.convert("RGB").save("photo.eps") # L, RGB or CMYK; no alpha
How to tell plain EPS from EPS with a binary preview#
Python
with open("logo.eps", "rb") as f:
head = f.read(4)
if head == b"\xc5\xd0\xd3\xc6":
print("binary header with TIFF/WMF preview")
elif head == b"%!PS":
print("plain PostScript text")
Tips#
scalemust be an integer. For exact pixel sizes, render a bit larger and resize down.- Set
EpsImagePlugin.gs_binaryto the full path of the Ghostscript executable if it isn’t onPATH. - Opening an EPS file without
%%BoundingBoxfails with “cannot determine EPS bounding box”.