我有很多 bash 相关的编程技能,但我的 Python 技能有所欠缺。我似乎无法做一些像让用户输入图像的两个参数那么简单的事情。
代码应该从不同的参数获取两个图像,然后在输出新图像之前对它们运行“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')
当尝试运行代码时,我收到此错误:
ubuntu@ubuntu-VirtualBox:/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
ubuntu@ubuntu-VirtualBox:/media/ubuntu/Transfer$
任何帮助将不胜感激。
最佳答案
@click
命令应与函数
一起使用
你的代码应该是这样的:
@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
关于Python参数语法错误: invalid syntax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42262376/