使用 Subprocess 处理来自 ImageMagick 的异常
Handle Exceptions from ImageMagick with Subprocess
我有一个子进程调用 convert from ImageMagik 将 gif 分割成帧,但是当它遇到像这样的坏 gif 时 (http://www.image-share.com/upload/3207/277.gif)
try:
command = ["convert" , "-coalesce", "-scene" , "0" , infile, outfile]
output,error = subprocess.Popen(command, universal_newlines=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
except:
print "%%% convert failed Up"
else:
print "*** converted"
我从来没有例外。文件未创建,打印输出和错误结果为 NULL
当您从命令行执行相同的命令时,您会收到此错误:
convert -coalesce examine.gif video.png convert: corrupt image examine.gif' @ error/gif.c/ReadGIFImage/1368.
convert: no images definedvideo.png' @ error/convert.c/ConvertImageCommand/3252.
所以它确实输出了一条错误消息 - 但我似乎无法捕获它!是否有针对不良 gif 和获取错误输出的解决方法?
我建议评估 subprocess.Popen.returncode
。在此示例中,应抛出(或引发)任何 none-零响应。
try:
command = ["convert" , "-coalesce", "-scene" , "0" , infile, outfile]
child = subprocess.Popen(command, universal_newlines=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output,error = child.communicate()
if child.returncode != 0:
raise Exception("Oops") # Although you should use a better Exception + message
except:
print( "%%%% convert failed Up")
else:
print( "*** converted")
我有一个子进程调用 convert from ImageMagik 将 gif 分割成帧,但是当它遇到像这样的坏 gif 时 (http://www.image-share.com/upload/3207/277.gif)
try:
command = ["convert" , "-coalesce", "-scene" , "0" , infile, outfile]
output,error = subprocess.Popen(command, universal_newlines=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
except:
print "%%% convert failed Up"
else:
print "*** converted"
我从来没有例外。文件未创建,打印输出和错误结果为 NULL
当您从命令行执行相同的命令时,您会收到此错误:
convert -coalesce examine.gif video.png convert: corrupt image examine.gif' @ error/gif.c/ReadGIFImage/1368.
convert: no images definedvideo.png' @ error/convert.c/ConvertImageCommand/3252.
所以它确实输出了一条错误消息 - 但我似乎无法捕获它!是否有针对不良 gif 和获取错误输出的解决方法?
我建议评估 subprocess.Popen.returncode
。在此示例中,应抛出(或引发)任何 none-零响应。
try:
command = ["convert" , "-coalesce", "-scene" , "0" , infile, outfile]
child = subprocess.Popen(command, universal_newlines=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output,error = child.communicate()
if child.returncode != 0:
raise Exception("Oops") # Although you should use a better Exception + message
except:
print( "%%%% convert failed Up")
else:
print( "*** converted")