Python参数SyntaxError:无效的语法
问题描述:
我有很多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$
任何帮助将不胜感激。
答
@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
感谢,代码工作吧! – ProGamerGov