mchd
mchd

Reputation: 3163

Function throws an argument error even though the correct number of arguments were supplied

I using PIL.ImageDraw.rectangle() to draw a bounding box around an image. I am supplying the top-left corner of the image, the image width and height and its outline colour. These are the correct parameters but I am getting this error:

TypeError: function takes at least 3 arguments (2 given)

This is my code:

img_draw = ImageDraw.Draw(logo)
logo_w, logo_h = logo.size
location = (logo_position[0], logo_position[1])

img_draw.rectangle(location,
        ((logo_position[0] + logo_w),
        (logo_position[1] + logo_h)), 
        outline='Red')  # Red bounding box around each logo

I checked it many times and this isn't an issue with the parentheses. However, I couldn't figure out the issue. The docs also confirm that my code is correct.

Upvotes: 0

Views: 754

Answers (1)

JohanL
JohanL

Reputation: 6891

The error message you get is peculiar, and wrong, one might say. There are some quirks in the way that arguments are parsed and sometimes you get strange error messages.

Your error is, in fact, a consequence of you passing too many arguments. The documentation states that the coordinates of the rectangle should be given as a single argument either as a list of four numbers, or as a list of two tuples.

In your code you have split the argument into two, where you are providing the coordinates individually. You should group them in a list, and it will work:

img_draw.rectangle([location,
        ((logo_position[0] + logo_w),
        (logo_position[1] + logo_h))], 
        outline='Red')  # Red bounding box around each logo

Note the extra brackets, [], included to combine your coordinates into a list.

Upvotes: 2

Related Questions