【问题标题】:Image Processing: Converting Image to Grayscale图像处理:将图像转换为灰度
【发布时间】:2021-02-01 03:50:31
【问题描述】:

我是 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

【问题讨论】:

  • 有趣的事实,大多数人使用这些来自模拟屏幕的权重,没有人考虑到数字屏幕已经被称重......如果没有,那么 ffffff(rgb 的最大强度)看起来不会是白色的,因此这些权重可能是多余的。无论如何,请注意,灰度 rgb 都需要相同的值。

标签: python image grayscale


【解决方案1】:

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

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    相关资源
    最近更新 更多