RGB图像到灰度

时间:2018-04-18 16:09:25

标签: python image-processing

对于我的一个类,使用Python将彩色图像转换为灰度而不使用库。到目前为止我已经完成了所有设置,除了转换我无法弄清楚并且没有找到任何解决方法。

这是我的代码:

from graphics import *

def main():

    print("This program will take a colored image \nand make make a new image in 
    greyscale")

    filename = input("What is the name of your image: ")
    savefile = input("What would you like the file saved as: ")

    win = GraphWin("Project 3", 640, 640)
    win.setCoords(0, 0, 640, 640)
    Text(Point(320, 320), "Click to begin!").draw(win)
    win.getMouse()
    picture = Image(Point(320, 320), filename)
    picture.draw(win)

    width = 640
    height = 640

    for i in range(width):
        for j in range(height):

1 个答案:

答案 0 :(得分:0)

在你的循环体内:

colors = picture.getPixel(i, j)
average = sum(colors) / 3.0
picture.setPixel(i, j, [average, average, average])