Skip to content

Commit 0a43254

Browse files
authored
Merge pull request #7392 from radarhere/eps
2 parents 54aa8fa + ffe2076 commit 0a43254

File tree

3 files changed

+47
-34
lines changed

3 files changed

+47
-34
lines changed

Tests/test_file_eps.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
assert_image_similar,
99
assert_image_similar_tofile,
1010
hopper,
11+
is_win32,
1112
mark_if_feature_version,
1213
skip_unless_feature,
1314
)
@@ -98,6 +99,20 @@ def test_load():
9899
assert im.load()[0, 0] == (255, 255, 255)
99100

100101

102+
def test_binary():
103+
if HAS_GHOSTSCRIPT:
104+
assert EpsImagePlugin.gs_binary is not None
105+
else:
106+
assert EpsImagePlugin.gs_binary is False
107+
108+
if not is_win32():
109+
assert EpsImagePlugin.gs_windows_binary is None
110+
elif not HAS_GHOSTSCRIPT:
111+
assert EpsImagePlugin.gs_windows_binary is False
112+
else:
113+
assert EpsImagePlugin.gs_windows_binary is not None
114+
115+
101116
def test_invalid_file():
102117
invalid_file = "Tests/images/flower.jpg"
103118
with pytest.raises(SyntaxError):

docs/handbook/image-file-formats.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,13 @@ in ``L``, ``RGB`` and ``CMYK`` modes.
115115
Loading
116116
~~~~~~~
117117

118+
To use Ghostscript, Pillow searches for the "gs" executable. On Windows, it
119+
also searches for "gswin32c" and "gswin64c". To customise this behaviour,
120+
``EpsImagePlugin.gs_binary = "gswin64"`` will set the name of the executable to
121+
use. ``EpsImagePlugin.gs_binary = False`` will prevent Ghostscript use.
122+
118123
If Ghostscript is available, you can call the :py:meth:`~PIL.Image.Image.load`
119-
method with the following parameters to affect how Ghostscript renders the EPS
124+
method with the following parameters to affect how Ghostscript renders the EPS.
120125

121126
**scale**
122127
Affects the scale of the resultant rasterized image. If the EPS suggests

src/PIL/EpsImagePlugin.py

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,39 @@
3737
split = re.compile(r"^%%([^:]*):[ \t]*(.*)[ \t]*$")
3838
field = re.compile(r"^%[%!\w]([^:]*)[ \t]*$")
3939

40+
gs_binary = None
4041
gs_windows_binary = None
41-
if sys.platform.startswith("win"):
42-
import shutil
43-
44-
for binary in ("gswin32c", "gswin64c", "gs"):
45-
if shutil.which(binary) is not None:
46-
gs_windows_binary = binary
47-
break
48-
else:
49-
gs_windows_binary = False
5042

5143

5244
def has_ghostscript():
53-
if gs_windows_binary:
54-
return True
55-
if not sys.platform.startswith("win"):
56-
try:
57-
subprocess.check_call(["gs", "--version"], stdout=subprocess.DEVNULL)
58-
return True
59-
except OSError:
60-
# No Ghostscript
61-
pass
62-
return False
45+
global gs_binary, gs_windows_binary
46+
if gs_binary is None:
47+
if sys.platform.startswith("win"):
48+
if gs_windows_binary is None:
49+
import shutil
50+
51+
for binary in ("gswin32c", "gswin64c", "gs"):
52+
if shutil.which(binary) is not None:
53+
gs_windows_binary = binary
54+
break
55+
else:
56+
gs_windows_binary = False
57+
gs_binary = gs_windows_binary
58+
else:
59+
try:
60+
subprocess.check_call(["gs", "--version"], stdout=subprocess.DEVNULL)
61+
gs_binary = "gs"
62+
except OSError:
63+
gs_binary = False
64+
return gs_binary is not False
6365

6466

6567
def Ghostscript(tile, size, fp, scale=1, transparency=False):
6668
"""Render an image using Ghostscript"""
69+
global gs_binary
70+
if not has_ghostscript():
71+
msg = "Unable to locate Ghostscript on paths"
72+
raise OSError(msg)
6773

6874
# Unpack decoder tile
6975
decoder, tile, offset, data = tile[0]
@@ -113,7 +119,7 @@ def Ghostscript(tile, size, fp, scale=1, transparency=False):
113119

114120
# Build Ghostscript command
115121
command = [
116-
"gs",
122+
gs_binary,
117123
"-q", # quiet mode
118124
"-g%dx%d" % size, # set output geometry (pixels)
119125
"-r%fx%f" % res, # set input DPI (dots per inch)
@@ -132,19 +138,6 @@ def Ghostscript(tile, size, fp, scale=1, transparency=False):
132138
"showpage",
133139
]
134140

135-
if gs_windows_binary is not None:
136-
if not gs_windows_binary:
137-
try:
138-
os.unlink(outfile)
139-
if infile_temp:
140-
os.unlink(infile_temp)
141-
except OSError:
142-
pass
143-
144-
msg = "Unable to locate Ghostscript on paths"
145-
raise OSError(msg)
146-
command[0] = gs_windows_binary
147-
148141
# push data through Ghostscript
149142
try:
150143
startupinfo = None

0 commit comments

Comments
 (0)