Purple82
Purple82

Reputation: 31

Python TypeError: integer argument expected, got float

Hello I am trying to run code that involves drawing a box but keeps returning the error

TypeError: integer argument expected, got float

The first issue in the code that appeared was

def draw_box(Image, x, y, w, h):
    cv2.line(Image, (x, y), (x + (w/5) ,y), WHITE, 2)
    cv2.line(Image, (x+((w/5)*4), y), (x+w, y), WHITE, 2)
    cv2.line(Image, (x, y), (x, y+(h/5)), WHITE, 2)
    cv2.line(Image, (x+w, y), (x+w, y+(h/5)), WHITE, 2)
    cv2.line(Image, (x, (y+(h/5*4))), (x, y+h), WHITE, 2)
    cv2.line(Image, (x, (y+h)), (x + (w/5) ,y+h), WHITE, 2)
    cv2.line(Image, (x+((w/5)*4), y+h), (x + w, y + h), WHITE, 2)
    cv2.line(Image, (x+w, (y+(h/5*4))), (x+w, y+h), WHITE, 2)

Which I fixed by replacing the division signs with python floor division, though then the next area of box drawing which returns the same Type Error

cv2.rectangle(Image, (Name_X_pos-10, Name_y_pos-25), (Name_X_pos +10 + (len(NAME) * 7), Name_y_pos-1), (0,0,0), -2)

I've tried putting cv2.rectangle(int(Image, (Name_.. only to receive back

TypeError: int() takes at most 2 arguments (5 given)

Any ideas on how to fix this?

Upvotes: 1

Views: 14189

Answers (1)

alkasm
alkasm

Reputation: 23052

As with the original error, the problem in your rectangle() call is that the arguments which specify pixel coordinates are not integers. Even though there's no division, it's not clear whether some of your original variables are floats or if multiplying just converted some of them to floats and not integers...either way, if you just cast each coordinate as an integer, you should be good to go. For e.g.:

cv2.rectangle(Image,
              (int(Name_X_pos-10), int(Name_y_pos-25)),
              (int(Name_X_pos +10 + (len(NAME) * 7)), int(Name_y_pos-1)),
              (0,0,0), -2)

Upvotes: 4

Related Questions