mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-11 17:56:18 +03:00
Added gs_binary to control Ghostscript use on all platforms
This commit is contained in:
parent
f225130ee5
commit
a84a43528e
|
@ -8,6 +8,7 @@ from .helper import (
|
||||||
assert_image_similar,
|
assert_image_similar,
|
||||||
assert_image_similar_tofile,
|
assert_image_similar_tofile,
|
||||||
hopper,
|
hopper,
|
||||||
|
is_win32,
|
||||||
mark_if_feature_version,
|
mark_if_feature_version,
|
||||||
skip_unless_feature,
|
skip_unless_feature,
|
||||||
)
|
)
|
||||||
|
@ -98,6 +99,20 @@ def test_load():
|
||||||
assert im.load()[0, 0] == (255, 255, 255)
|
assert im.load()[0, 0] == (255, 255, 255)
|
||||||
|
|
||||||
|
|
||||||
|
def test_binary():
|
||||||
|
if HAS_GHOSTSCRIPT:
|
||||||
|
assert EpsImagePlugin.gs_binary is not None
|
||||||
|
else:
|
||||||
|
assert EpsImagePlugin.gs_binary is False
|
||||||
|
|
||||||
|
if not is_win32():
|
||||||
|
assert EpsImagePlugin.gs_windows_binary is None
|
||||||
|
elif not HAS_GHOSTSCRIPT:
|
||||||
|
assert EpsImagePlugin.gs_windows_binary is False
|
||||||
|
else:
|
||||||
|
assert EpsImagePlugin.gs_windows_binary is not None
|
||||||
|
|
||||||
|
|
||||||
def test_invalid_file():
|
def test_invalid_file():
|
||||||
invalid_file = "Tests/images/flower.jpg"
|
invalid_file = "Tests/images/flower.jpg"
|
||||||
with pytest.raises(SyntaxError):
|
with pytest.raises(SyntaxError):
|
||||||
|
|
|
@ -37,40 +37,39 @@ from ._deprecate import deprecate
|
||||||
split = re.compile(r"^%%([^:]*):[ \t]*(.*)[ \t]*$")
|
split = re.compile(r"^%%([^:]*):[ \t]*(.*)[ \t]*$")
|
||||||
field = re.compile(r"^%[%!\w]([^:]*)[ \t]*$")
|
field = re.compile(r"^%[%!\w]([^:]*)[ \t]*$")
|
||||||
|
|
||||||
|
gs_binary = None
|
||||||
gs_windows_binary = None
|
gs_windows_binary = None
|
||||||
|
|
||||||
|
|
||||||
def _get_windows_binary():
|
|
||||||
global gs_windows_binary
|
|
||||||
if gs_windows_binary is not None:
|
|
||||||
return gs_windows_binary
|
|
||||||
if not sys.platform.startswith("win"):
|
|
||||||
return
|
|
||||||
import shutil
|
|
||||||
|
|
||||||
for binary in ("gswin32c", "gswin64c", "gs"):
|
|
||||||
if shutil.which(binary) is not None:
|
|
||||||
gs_windows_binary = binary
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
gs_windows_binary = False
|
|
||||||
return gs_windows_binary
|
|
||||||
|
|
||||||
|
|
||||||
def has_ghostscript():
|
def has_ghostscript():
|
||||||
gs_windows_binary = _get_windows_binary()
|
global gs_binary, gs_windows_binary
|
||||||
if gs_windows_binary is not None:
|
if gs_binary is None:
|
||||||
return gs_windows_binary is not False
|
if sys.platform.startswith("win"):
|
||||||
try:
|
if gs_windows_binary is None:
|
||||||
subprocess.check_call(["gs", "--version"], stdout=subprocess.DEVNULL)
|
import shutil
|
||||||
return True
|
|
||||||
except OSError:
|
for binary in ("gswin32c", "gswin64c", "gs"):
|
||||||
# No Ghostscript
|
if shutil.which(binary) is not None:
|
||||||
return False
|
gs_windows_binary = binary
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
gs_windows_binary = False
|
||||||
|
gs_binary = gs_windows_binary
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
subprocess.check_call(["gs", "--version"], stdout=subprocess.DEVNULL)
|
||||||
|
gs_binary = "gs"
|
||||||
|
except OSError:
|
||||||
|
gs_binary = False
|
||||||
|
return gs_binary is not False
|
||||||
|
|
||||||
|
|
||||||
def Ghostscript(tile, size, fp, scale=1, transparency=False):
|
def Ghostscript(tile, size, fp, scale=1, transparency=False):
|
||||||
"""Render an image using Ghostscript"""
|
"""Render an image using Ghostscript"""
|
||||||
|
global gs_binary
|
||||||
|
if not has_ghostscript():
|
||||||
|
msg = "Unable to locate Ghostscript on paths"
|
||||||
|
raise OSError(msg)
|
||||||
|
|
||||||
# Unpack decoder tile
|
# Unpack decoder tile
|
||||||
decoder, tile, offset, data = tile[0]
|
decoder, tile, offset, data = tile[0]
|
||||||
|
@ -120,7 +119,7 @@ def Ghostscript(tile, size, fp, scale=1, transparency=False):
|
||||||
|
|
||||||
# Build Ghostscript command
|
# Build Ghostscript command
|
||||||
command = [
|
command = [
|
||||||
"gs",
|
gs_binary,
|
||||||
"-q", # quiet mode
|
"-q", # quiet mode
|
||||||
"-g%dx%d" % size, # set output geometry (pixels)
|
"-g%dx%d" % size, # set output geometry (pixels)
|
||||||
"-r%fx%f" % res, # set input DPI (dots per inch)
|
"-r%fx%f" % res, # set input DPI (dots per inch)
|
||||||
|
@ -139,20 +138,6 @@ def Ghostscript(tile, size, fp, scale=1, transparency=False):
|
||||||
"showpage",
|
"showpage",
|
||||||
]
|
]
|
||||||
|
|
||||||
gs_windows_binary = _get_windows_binary()
|
|
||||||
if gs_windows_binary is False:
|
|
||||||
try:
|
|
||||||
os.unlink(outfile)
|
|
||||||
if infile_temp:
|
|
||||||
os.unlink(infile_temp)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
msg = "Unable to locate Ghostscript on paths"
|
|
||||||
raise OSError(msg)
|
|
||||||
elif gs_windows_binary is not None:
|
|
||||||
command[0] = gs_windows_binary
|
|
||||||
|
|
||||||
# push data through Ghostscript
|
# push data through Ghostscript
|
||||||
try:
|
try:
|
||||||
startupinfo = None
|
startupinfo = None
|
||||||
|
|
Loading…
Reference in New Issue
Block a user