尝试解析参数Python shell时出现语法错误

时间:2015-03-23 15:51:58

标签: python shell syntax-error argparse args

我有一些代码,我试图在Python shell(IDLE)中运行但是我在Python shell中解析参数的方式似乎有问题。

以下是代码:

# import the necessary packages
from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
from skimage import io
import matplotlib.pyplot as plt
import argparse

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())

# load the image and convert it to a floating point data type
image = img_as_float(io.imread(args["image"]))

# loop over the number of segments
for numSegments in (100, 200, 300):
    # apply SLIC and extract (approximately) the supplied number
    # of segments
    segments = slic(image, n_segments = numSegments, sigma = 5)

    # show the output of SLIC
    fig = plt.figure("Superpixels -- %d segments" % (numSegments))
    ax = fig.add_subplot(1, 1, 1)
    ax.imshow(mark_boundaries(image, segments))
    plt.axis("off")

# show the plots
plt.show()

当我尝试使用行Slic.py --image 0021.jpg运行程序时,我得到SyntaxError: invalid syntax。不确定我做错了什么,可能对某些人来说非常明显,但非常感谢任何帮助。

代码在页面底部的SLIC示例下面的链接中找到,他甚至展示了如何运行代码,但它对我不起作用:

www.pyimagesearch.com/2014/07/28/a-slic-superpixel-tutorial-using-python

1 个答案:

答案 0 :(得分:0)

只是一个想法 - 这可能是与IDLE的冲突。尝试从shell运行(您可能已经猜到了,python Slic.py --image 0021.jpg)。当然,由于这是一个5个月大的问题,我认为你现在已经解决了这个问题:)