使用子进程处理来自ImageMagick的异常

2024-09-25 04:26:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个子进程调用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"

我从来没有例外。文件未创建,打印输出和错误结果为空

从命令行执行相同的命令时,会出现以下错误:

^{pr2}$

所以它确实输出了一个错误消息-但我似乎无法捕捉它!有没有办法解决坏的gif和错误输出?在


Tags: fromimagehttpshareconvert进程www错误
1条回答
网友
1楼 · 发布于 2024-09-25 04:26:49

我建议评估subprocess.Popen.returncode。在本例中,应该抛出(或引发)任何非零响应。在

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")

相关问题 更多 >