mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-03 11:23:05 +03:00
Fix hang by using temp file and command line args instead of stdin
This commit is contained in:
parent
8ce2faa8c9
commit
a549e77bd8
|
@ -67,16 +67,29 @@ def Ghostscript(tile, size, fp, scale=1):
|
||||||
|
|
||||||
import tempfile, os, subprocess
|
import tempfile, os, subprocess
|
||||||
|
|
||||||
file = tempfile.mktemp()
|
outfile = tempfile.mktemp()
|
||||||
|
infile = tempfile.mktemp()
|
||||||
|
|
||||||
|
with open(infile, 'wb') as f:
|
||||||
|
fp.seek(offset)
|
||||||
|
while length >0:
|
||||||
|
s = fp.read(100*1024)
|
||||||
|
if not s:
|
||||||
|
break
|
||||||
|
length = length - len(s)
|
||||||
|
f.write(s)
|
||||||
|
|
||||||
# Build ghostscript command
|
# Build ghostscript command
|
||||||
command = ["gs",
|
command = ["gs",
|
||||||
"-q", # quite mode
|
"-q", # quiet mode
|
||||||
"-g%dx%d" % size, # set output geometry (pixels)
|
"-g%dx%d" % size, # set output geometry (pixels)
|
||||||
"-r%d" % (72*scale), # set input DPI (dots per inch)
|
"-r%d" % (72*scale), # set input DPI (dots per inch)
|
||||||
"-dNOPAUSE -dSAFER", # don't pause between pages, safe mode
|
"-dNOPAUSE -dSAFER", # don't pause between pages, safe mode
|
||||||
"-sDEVICE=ppmraw", # ppm driver
|
"-sDEVICE=ppmraw", # ppm driver
|
||||||
"-sOutputFile=%s" % file,# output file
|
"-sOutputFile=%s" % outfile, # output file
|
||||||
|
"-c", "%d %d translate" % (-bbox[0], -bbox[1]),
|
||||||
|
# adjust for image origin
|
||||||
|
"-f", infile, # input file
|
||||||
]
|
]
|
||||||
|
|
||||||
if gs_windows_binary is not None:
|
if gs_windows_binary is not None:
|
||||||
|
@ -87,23 +100,15 @@ def Ghostscript(tile, size, fp, scale=1):
|
||||||
# push data through ghostscript
|
# push data through ghostscript
|
||||||
try:
|
try:
|
||||||
gs = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
gs = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||||
# adjust for image origin
|
|
||||||
if bbox[0] != 0 or bbox[1] != 0:
|
|
||||||
gs.stdin.write(("%d %d translate\n" % (-bbox[0], -bbox[1])).encode('ascii'))
|
|
||||||
fp.seek(offset)
|
|
||||||
while length > 0:
|
|
||||||
s = fp.read(8192)
|
|
||||||
if not s:
|
|
||||||
break
|
|
||||||
length = length - len(s)
|
|
||||||
gs.stdin.write(s)
|
|
||||||
gs.stdin.close()
|
gs.stdin.close()
|
||||||
status = gs.wait()
|
status = gs.wait()
|
||||||
if status:
|
if status:
|
||||||
raise IOError("gs failed (status %d)" % status)
|
raise IOError("gs failed (status %d)" % status)
|
||||||
im = Image.core.open_ppm(file)
|
im = Image.core.open_ppm(outfile)
|
||||||
finally:
|
finally:
|
||||||
try: os.unlink(file)
|
try:
|
||||||
|
os.unlink(outfile)
|
||||||
|
os.unlink(infile)
|
||||||
except: pass
|
except: pass
|
||||||
|
|
||||||
return im
|
return im
|
||||||
|
|
Loading…
Reference in New Issue
Block a user