1

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?

6
  • You don't want int(Image, (Name)..) -- you want to cast the individual numbers as integers, so for e.g. (int(Name_X_pos+10+len(NAME)*7), ...) Commented Dec 5, 2017 at 0:04
  • I did that and it now returned the error "ValueError: int() base must be >= 2 and <= 36" Commented Dec 5, 2017 at 0:07
  • You're including multiple arguments in int(). Just do one number at a time (look carefully at the code I just sent). Commented Dec 5, 2017 at 0:09
  • 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) Commented Dec 5, 2017 at 0:11
  • That fixed it. I appreciate the help Commented Dec 5, 2017 at 0:14

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.