TypeError: integer argument expected, got float when converting an image to greyscale

TypeError: integer argument expected, got float when converting an image to greyscale

我正在使用以下代码将彩色图像转换为灰度图像。为什么会抛出 TypeError?

#!/usr/bin/python
from PIL import Image
im = Image.open("Penguins.jpg")
pixel = im.load()
width, height = im.size
for x in range(width):
    for y in range(height):
        R,G,B = pixel[x,y]
        pixel[x,y] = ((0.299*R+0.587*G+0.114*B),(0.299*R+0.587*G+0.114*B),(0.299*R+0.587*G+0.114*B))

im.save("Penguins_new.jpg")

您传递给 pixel[x, y] 的参数必须是 int,而不是 float。尝试将其转换为整数。

pixel[x, y] = ((int(0.299*R) + int(...