I have some code that I am trying to run in a Python shell (IDLE) but there seems to be a problem with the way I am parsing arguments in the Python shell.

Here is the code:

# 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()

When I try to run the program with the line Slic.py --image 0021.jpg I get SyntaxError: invalid syntax. Not sure what I am doing wrong, probably really obvious to some but any help is greatly appreciated.

The code was found at the link below under the SLIC example at the bottom of the page, he even shows how to run the code but it doesn't work for me:

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

share|improve this question
    
Does it tell you where the error is? What version of python are you using? – Etan Reisner Mar 23 '15 at 15:55
    
@EtanReisner im using Python 2.7.5 and the error doesn't seem to be on anything specific, as when I try using a .jgp file it says that the 'j' is invalid syntax and when I use a .png file it says that the '.' (full stop) is where the error is. – Neil Vicker Mar 23 '15 at 15:58
    
@EtanReisner just to be clear the invalid syntax doesn't seem to be in the actual code, but instead in the arguments that I am trying to parse. – Neil Vicker Mar 23 '15 at 16:02
    
what happens if you pass the argument in "quotes"? – Kells1986 Mar 23 '15 at 16:08
1  
Shell quoting is not the issue. I think the issue here might be that the default type of an argument in argparse is an integer and you need to tell it to expect a string (or that it sees the digits and assumes an integer and then blows up on the non-digit input). The documentation seems to indicate otherwise though. But try adding type=file or type=str to the add_argument call? – Etan Reisner Mar 23 '15 at 16:17

Just a thought - this may be a clash with IDLE. Try running from the shell (as you probably have guessed, python Slic.py --image 0021.jpg). Of course, since this is a 5 month old question, I assume that you've resolved it by now :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.