mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-12 10:16:17 +03:00
Better error checking in _save_netpbm
This commit is contained in:
parent
b5ab5a49e7
commit
a301d061fb
|
@ -333,16 +333,32 @@ def _save_netpbm(im, fp, filename):
|
|||
# below for information on how to enable this.
|
||||
|
||||
import os
|
||||
from subprocess import Popen, check_call, PIPE
|
||||
from subprocess import Popen, check_call, PIPE, CalledProcessError
|
||||
file = im._dump()
|
||||
if im.mode != "RGB":
|
||||
with open(filename, 'wb') as f:
|
||||
check_call(["ppmtogif", file], stdout=f)
|
||||
else:
|
||||
with open(filename, 'wb') as f:
|
||||
#TODO: What happens if ppmquant fails?
|
||||
ppmquant_proc = Popen(["ppmquant", "256", file], stdout=PIPE)
|
||||
check_call(["ppmtogif"], stdin=ppmquant_proc.stdout, stdout=f)
|
||||
|
||||
# Pipe ppmquant output into ppmtogif
|
||||
# "ppmquant 256 %s | ppmtogif > %s" % (file, filename)
|
||||
quant_cmd = ["ppmquant", "256", file]
|
||||
togif_cmd = ["ppmtogif"]
|
||||
quant_proc = Popen(quant_cmd, stdout=PIPE)
|
||||
togif_proc = Popen(togif_cmd, stdin=quant_proc.stdout, stdout=f)
|
||||
|
||||
# Allow ppmquant to receive SIGPIPE if ppmtogif exits
|
||||
quant_proc.stdout.close()
|
||||
|
||||
retcode = quant_proc.wait()
|
||||
if retcode:
|
||||
raise CalledProcessError(retcode, quant_cmd)
|
||||
|
||||
retcode = togif_proc.wait()
|
||||
if retcode:
|
||||
raise CalledProcessError(retcode, togif_cmd)
|
||||
|
||||
try:
|
||||
os.unlink(file)
|
||||
except:
|
||||
|
|
Loading…
Reference in New Issue
Block a user