Better error checking in _save_netpbm

This commit is contained in:
Michael Brown 2014-06-27 11:02:36 -04:00
parent b5ab5a49e7
commit a301d061fb

View File

@ -333,16 +333,32 @@ def _save_netpbm(im, fp, filename):
# below for information on how to enable this. # below for information on how to enable this.
import os import os
from subprocess import Popen, check_call, PIPE from subprocess import Popen, check_call, PIPE, CalledProcessError
file = im._dump() file = im._dump()
if im.mode != "RGB": if im.mode != "RGB":
with open(filename, 'wb') as f: with open(filename, 'wb') as f:
check_call(["ppmtogif", file], stdout=f) check_call(["ppmtogif", file], stdout=f)
else: else:
with open(filename, 'wb') as f: with open(filename, 'wb') as f:
#TODO: What happens if ppmquant fails?
ppmquant_proc = Popen(["ppmquant", "256", file], stdout=PIPE) # Pipe ppmquant output into ppmtogif
check_call(["ppmtogif"], stdin=ppmquant_proc.stdout, stdout=f) # "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: try:
os.unlink(file) os.unlink(file)
except: except: