Don't understand this SyntaxError: illegal target for annotation

24,473

Look at these lines:

If (y1 < 0): y_movedown()
Elif (y2 > imageheight): y_moveup()
Elif (x1 < 0) : x_moveright()
Elif (x2 > imagewidth): x_moveleft()

If, Elif are titled words, when they should be lowercase, so do:

if (y1 < 0): y_movedown()
elif (y2 > imageheight): y_moveup()
elif (x1 < 0) : x_moveright()
elif (x2 > imagewidth): x_moveleft()

Instead.

Share:
24,473
powderscotty
Author by

powderscotty

Updated on July 09, 2022

Comments

  • powderscotty
    powderscotty almost 2 years

    I have some simple if... elif... in my python3.6 (& OpenCV 4.0), but no matter what I do I keep getting strange error messages.

    I need to crop some pics according to some bounding boxes.

    # the image to be cropped loads here:
    tobecropped= cv2.imread(img)
    images.append(tobecropped)
    (imageheight, imagewidth) = tobecropped.shape[:2]
    
    # sample bounding box:
    y = 833 # center vertically
    x = 183 # center horizontally
    width = 172
    height = 103
    
    # calculation of cropping values adding 10% extra:
    y1 = y-(height/2*1.1)
    y2 = y+(height/2*1.1)
    x1 = x-(width/2*1.1)
    x2 = x+(width/2*1.1)
    
    # return values to int:
    y1 = int(y1)
    y2 = int(y2)
    x1 = int(x1)
    x2 = int(x2)
    
    # move the cropping inside the original image:
    If (y1 < 0): y_movedown()
    Elif (y2 > imageheight): y_moveup()
    Elif (x1 < 0) : x_moveright()
    Elif (x2 > imagewidth): x_moveleft()
    
    # actually cropping the image:
    cropped = tobecropped[y1:y2, x1:x2]
    cv2.imshow("cropped", cropped)
    
    # functions to move the complete bounding box inside the image boundaries:
    def y_movedown():
        y2=y2-y1
        y1=0
        print("moved down!")
    
    def y_moveup():
        y1=y1-(y2-imageheight)
        y2=imageheight
        print("moved up!")
    
    def x_moveright():
        x2=x2-x1
        x1=0
        print("moved right!")
    
    def x_moveleft():
        x1=x1-(x2-imagewidth)
        x2=imagewidth
        print("moved left!")
    

    the error message looks like this:

    File "image_import2.py", line 121
       If (y1 < 0): y_movedown()
       ^
    SyntaxError: illegal target for annotation
    

    Does anyone see, what I am doing wrong here? Could not find any mention of such a mistake...

  • U12-Forward
    U12-Forward over 5 years
    @powderscotty Please accept and upvote if it works, thanks, happy to help :-),
  • Zezombye
    Zezombye almost 3 years
    The more general answer is that you have a ":" at the end of a line, while the line doesn't begin with an "if", "elif", "else", "for", etc.