2014-01-28 71 views
0

我正在嘗試使用PIL來實現一個小Filtres,並且已經到了這麼遠,但是我被這個回溯卡住了。Python子進程錯誤

Traceback (most recent call last): 
    File "C:/Users/Ajay/PycharmProjects/pygram/test.py", line 5, in <module> 
    pg.execute("convert {filename} -modulate 120,10,100 -fill '#222b6d' -colorize 20 -gamma 0.5 -contrast -contrast {filename}") 
    File "C:\Users\Ajay\PycharmProjects\pygram\pygram.py", line 22, in execute 
    error = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT) 
    File "C:\Python27\lib\subprocess.py", line 575, in check_output 
    raise CalledProcessError(retcode, cmd, output=output) 
subprocess.CalledProcessError: Command 'convert git.jpg -modulate 120,10,100 -fill '#222b6d' -colorize 20 -gamma 0.5 -contrast -contrast git.jpg' returned non-zero exit status 4 

代碼

import subprocess, math, os, inspect 
from PIL import Image 

class PyGram: 
    def __init__(self, filename): 
     self.filename = filename 
     self.im = False 

    def image(self): 
     if not self.im: 
      self.im = Image.open(self.filename) 
     return self.im 

    def execute(self, command, **kwargs): 
     default = dict(
      filename=self.filename, 
      width=self.image().size[0], 
      height=self.image().size[1] 
     ) 
     format = dict(default.items() + kwargs.items()) 
     command = command.format(**format) 
     error = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT) 
     return error 

    def colortone(self, color, level, type=0): 
     arg0 = level 
     arg1 = 100 - level 
     if type == 0: 
      negate = '-negate' 
     else: 
      negate = '' 

     self.execute(
      "convert {filename} \(-clone 0 -fill '{color}' -colorize 100% \) \(-clone 0 -colorspace gray {negate} \) -compose blend -define compose:args={arg0},{arg1} -composite {filename}", 
      color=color, 
      negate=negate, 
      arg0=arg0, 
      arg1=arg1 
     ) 

    def border(self, color='black', width=20): 
     self.execute("convert {filename} -bordercolor {color} -border {bwidth}x{bwidth} {filename}", 
        color=color, 
        bwidth=width 
     ) 

test.py

from pygram import PyGram 

pg = PyGram("git.jpg") 

pg.execute("convert {filename} -modulate 120,10,100 -fill '#222b6d' -colorize 20 -gamma 0.5 -contrast -contrast {filename}") 
pg.border() 

請幫幫忙,我哪裏做錯了嗎?

+1

如果直接在控制檯中運行問題命令,會發生什麼情況?在您的Windows路徑中「轉換」可執行文件? – Tom

+0

@Tom我不確定你在問什麼? – ajkumar25

+0

Python正試圖從你正在運行該文件的目錄中運行'convert git.jpg -modulate 120,10,100 -fill'#222b6d'-colorize 20 -gamma 0.5 -contrast -contrast git.jpg'。如果直接在同一目錄的命令提示符下運行該命令,會發生什麼情況? – Tom

回答

2

您的測試不正確。

您嘗試執行字符串:

"convert {filename} -modulate 120,10,100 -fill '#222b6d' -colorize 20 -gamma 0.5 -contrast -contrast {filename}" 

,但你並沒有在「文件名」的值傳遞。請參閱此處以獲取更多詳細信息:String Formatting in Python 3

+0

'{filename}'正確更改爲'git.jpg'.BTW不起作用 – ajkumar25

+1

輸出文件名不應與輸入文件名相同,您應該測試並確保該命令可以在DOS提示符或shell中獨立運行,這將使調試更容易。 –