Skip to content

Commit 6d1dcb6

Browse files
committed
Added gs_binary to control Ghostscript use on all platforms
1 parent f225130 commit 6d1dcb6

File tree

2 files changed

+39
-41
lines changed

2 files changed

+39
-41
lines changed

Tests/test_file_eps.py

Lines changed: 13 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,18 @@ 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 None
107+
108+
if HAS_GHOSTSCRIPT and is_win32():
109+
assert EpsImagePlugin.gs_windows_binary is not None
110+
else:
111+
assert EpsImagePlugin.gs_windows_binary is None
112+
113+
101114
def test_invalid_file():
102115
invalid_file = "Tests/images/flower.jpg"
103116
with pytest.raises(SyntaxError):

src/PIL/EpsImagePlugin.py

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,40 +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
4142

4243

43-
def _get_windows_binary():
44-
global gs_windows_binary
45-
if gs_windows_binary is not None:
46-
return gs_windows_binary
47-
if not sys.platform.startswith("win"):
48-
return
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-
return gs_windows_binary
58-
59-
6044
def has_ghostscript():
61-
gs_windows_binary = _get_windows_binary()
62-
if gs_windows_binary is not None:
63-
return gs_windows_binary is not False
64-
try:
65-
subprocess.check_call(["gs", "--version"], stdout=subprocess.DEVNULL)
66-
return True
67-
except OSError:
68-
# No Ghostscript
69-
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
7065

7166

7267
def Ghostscript(tile, size, fp, scale=1, transparency=False):
7368
"""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)
7473

7574
# Unpack decoder tile
7675
decoder, tile, offset, data = tile[0]
@@ -120,7 +119,7 @@ def Ghostscript(tile, size, fp, scale=1, transparency=False):
120119

121120
# Build Ghostscript command
122121
command = [
123-
"gs",
122+
gs_binary,
124123
"-q", # quiet mode
125124
"-g%dx%d" % size, # set output geometry (pixels)
126125
"-r%fx%f" % res, # set input DPI (dots per inch)
@@ -139,20 +138,6 @@ def Ghostscript(tile, size, fp, scale=1, transparency=False):
139138
"showpage",
140139
]
141140

142-
gs_windows_binary = _get_windows_binary()
143-
if gs_windows_binary is False:
144-
try:
145-
os.unlink(outfile)
146-
if infile_temp:
147-
os.unlink(infile_temp)
148-
except OSError:
149-
pass
150-
151-
msg = "Unable to locate Ghostscript on paths"
152-
raise OSError(msg)
153-
elif gs_windows_binary is not None:
154-
command[0] = gs_windows_binary
155-
156141
# push data through Ghostscript
157142
try:
158143
startupinfo = None

0 commit comments

Comments
 (0)