图像处理:将图像转换为灰度

Image Processing: Converting Image to Grayscale

我是 Python 的新手,在谷歌搜索公式后无法将此图像转换为灰度。我应用错了吗?无论我尝试什么,我的图像都是绿色的。

0.2989 * R + 0.5870 * G + 0.1140 * B

    import image

img= image.Image("luther.jpg")
win= image.ImageWin(img.getWidth(), img.getHeight())
img.draw(win)
img.setDelay(1,100)

for row in range(img.getHeight()):
    for col in range(img.getWidth()):
        p=img.getPixel(col, row)
        newRed= 0.2989*p.getRed()
        newGreen= 0.5870*p.getGreen()
        newBlue= 0.1140*p.getBlue()
        newpixel= image.Pixel(newRed, newGreen, newBlue)
        img.setPixel(col, row, newpixel)
        
img.draw(win)
win.exitonclick()enter code here

好的,我已经弄明白了。我试图在没有任何库的情况下解决这个问题,这是我的简单解决方案:

import image

img= image.Image("luther.jpg")
win= image.ImageWin(img.getWidth(), img.getHeight())
img.draw(win)
img.setDelay(1,100)

for row in range(img.getHeight()):
    for col in range(img.getWidth()):
        p=img.getPixel(col, row)
        grayscale= (p.getRed()+ p.getGreen()+ p.getBlue())/3
        newRed= grayscale
        newGreen= grayscale
        newBlue= grayscale
        newpixel= image.Pixel(newRed, newGreen, newBlue)
        img.setPixel(col, row, newpixel)
        
img.draw(win)
win.exitonclick()