PIL TypeError: text() has multiple values for argument 'font'

PIL TypeError: text() has multiple values for argument 'font'

我有函数:

imagewidth = 250 
y_positions = [65.0, 85.0, 105.0, 125.0, 145.0, 165.0] 
numbers_to_show = [20, 30, 40, 50, 60, 70]

def draw_numbers(imagewidth, y_positions, numbers_to_show):
    counter = 0
    while counter < len(y_positions):
        numbers_to_show = str(numbers_to_show[counter])
        font = ImageFont.truetype("impact.ttf",20)
        img = Image.open("agepyramid.bmp")
        draw = ImageDraw.Draw(img)
        draw.text(20 , y_positions[counter],numbers_to_show,(0,0,0),font=font)
        draw = ImageDraw.Draw(img)
        img.save("agepyramid.bmp")
        counter = counter + 1

我认为这个函数应该给我一张在图像的 "y_position" 处带有 "numbers_to_show" 的图片,但它给我这个错误:

draw.text(20 , y_positions[counter],numbers_to_show,(0,0,0),font=font)
TypeError: text() got multiple values for argument 'font'

我做错了什么?

Draw.text的第4个参数是font。您为 font 参数提供了 2 个值 - (0, 0, 0)(按位置)和 font(按名称),这会使解释器感到困惑。

我猜你想要

draw.text((20 , y_positions[counter]), numbers_to_show, (0, 0, 0), font=font)

其中 (0, 0, 0)fill 参数的值。