2017-02-15 141 views
0

我有很多bash相關的編程技巧,但是我的Python技能缺乏。我似乎無法像使用兩個參數一樣簡單地爲用戶輸入圖像。Python參數SyntaxError:無效的語法

該代碼應該從單獨的參數中獲取兩個圖像,然後在輸出新圖像之前使用它們運行「match_color」函數。

這裏是我的代碼:

from types import SimpleNamespace 
import argparse 

@click.argument('-target_image', type=click.Path(exists=True)) 
@click.argument('-source_image', type=click.Path(exists=True)) 
#Specify the name of the output file. 
#@click.argument('-out_file', type=click.Path(exists=True)) 



parser = argparse.ArgumentParser() 
args.parser.parse_args 
args = SimpleNamespace(**kwargs) 
target_img = Image.open(args.target_image) 
source_img = Image.open(args.source_image) 




def match_color(target_img, source_img, mode='pca', eps=1e-5): 
    ''' 
    Matches the colour distribution of the target image to that of the source image 
    using a linear transform. 
    Images are expected to be of form (w,h,c) and float in [0,1]. 
    Modes are chol, pca or sym for different choices of basis. 
    ''' 
    mu_t = target_img.mean(0).mean(0) 
    t = target_img - mu_t 
    t = t.transpose(2,0,1).reshape(3,-1) 
    Ct = t.dot(t.T)/t.shape[1] + eps * eye(t.shape[0]) 
    mu_s = source_img.mean(0).mean(0) 
    s = source_img - mu_s 
    s = s.transpose(2,0,1).reshape(3,-1) 
    Cs = s.dot(s.T)/s.shape[1] + eps * eye(s.shape[0]) 
    if mode == 'chol': 
     chol_t = np.linalg.cholesky(Ct) 
     chol_s = np.linalg.cholesky(Cs) 
     ts = chol_s.dot(np.linalg.inv(chol_t)).dot(t) 
    if mode == 'pca': 
     eva_t, eve_t = np.linalg.eigh(Ct) 
     Qt = eve_t.dot(np.sqrt(np.diag(eva_t))).dot(eve_t.T) 
     eva_s, eve_s = np.linalg.eigh(Cs) 
     Qs = eve_s.dot(np.sqrt(np.diag(eva_s))).dot(eve_s.T) 
     ts = Qs.dot(np.linalg.inv(Qt)).dot(t) 
    if mode == 'sym': 
     eva_t, eve_t = np.linalg.eigh(Ct) 
     Qt = eve_t.dot(np.sqrt(np.diag(eva_t))).dot(eve_t.T) 
     Qt_Cs_Qt = Qt.dot(Cs).dot(Qt) 
     eva_QtCsQt, eve_QtCsQt = np.linalg.eigh(Qt_Cs_Qt) 
     QtCsQt = eve_QtCsQt.dot(np.sqrt(np.diag(eva_QtCsQt))).dot(eve_QtCsQt.T) 
     ts = np.linalg.inv(Qt).dot(QtCsQt).dot(np.linalg.inv(Qt)).dot(t) 
    matched_img = ts.reshape(*target_img.transpose(2,0,1).shape).transpose(1,2,0) 
    matched_img += mu_s 
    matched_img[matched_img>1] = 1 
    matched_img[matched_img<0] = 0 
    return matched_img 

matched_img.save('out.png') 

當試圖運行的代碼,我得到這個錯誤:

[email protected]:/media/ubuntu/Transfer$ python3 linear-color-transfer.py -target_image fig4_style3.jpg -source_image fig4_content.jpg 
    File "linear-color-transfer.py", line 11 
    parser = argparse.ArgumentParser() 
     ^
SyntaxError: invalid syntax 
[email protected]:/media/ubuntu/Transfer$ 

任何幫助將不勝感激。

回答

1

@click命令應該functions

您的代碼中使用應該是這樣的:

@click.argument('-target_image', type=click.Path(exists=True)) 
@click.argument('-source_image', type=click.Path(exists=True)) 
def match_color(target_img, source_img, mode='pca', eps=1e-5): 
    #body 
+0

感謝,代碼工作吧! – ProGamerGov