Zelle's Simple Python Graphics Library RGB to Grayscale Conversion Project

1.1k Views Asked by At

So I have been tasked with a project to convert an image of my choosing from color to grayscale using only the graphics library provided in the link (http://mcsp.wartburg.edu/zelle/python/). I discovered another post here on StackOverFlow (Python 3: Converting image to grayscale) that gave me a basis from which to start, however I unfortunately have not been able to work out all the kinks and need a second pair of eyes to help me. I am also unsure of how to prompt the grayscale image to pop up on the screen so the user can have an idea of what the saved file will look like.

As of right now, the program runs smoothly in that it runs through the entire code without a hiccup, but it does not save a grayscale image.

I'd also like to say sorry in advance if this code does not look organized. I am new to programming and posting on forums so I apologize in advance. Thank you for your help as it was greatly needed and much appreciated.

from graphics import *

def colorless_picture_converter():
    file_name_input = input("Please input file name here: ")
    file_name_output = input("Save to: ")

    image = Image(Point(0, 0),file_name_input)
    width = int(image.getWidth())
    width_center = int(width / 2)
    height = int(image.getHeight())
    height_center = int(height / 2)
    image = Image(Point(width_center, height_center),file_name_input)
    image_window = GraphWin("Colorless Converter", width, height)
    image.draw(image_window)

    row = 0
    column = 0

    for row in range(width):
        for column in range(height):
            red_value, green_value, blue_value = image.getPixel(column, row)
            gray_set = int(round(0.299 * red_value + 0.587 * green_value + 0.114 * blue_value))
            image.setPixel(column, row, color_rgb(gray_set, gray_set, gray_set))
            image_window.update()
            image.save(file_name_output)
            image_window.close()
            return file_name_output

colorless_picture_converter()
1

There are 1 best solutions below

0
On

I too have been working with Zelles graphics to do grayscale image manipulation. I was able to get your code working by switching the width and height in the for loops and reducing the indents on the image.save(file_name_output), image_window close() and return file_name_output as shown below... (I also added a image_window.getMouse() method to keep the window open after the conversion to grayscale.) NOTE: I am using Python 3.4.

from graphics import *

def colorless_picture_converter():
    file_name_input = input("Please input file name here: ")
    file_name_output = input("Save to: ")

    image = Image(Point(0, 0),file_name_input)
    width = int(image.getWidth())
    width_center = int(width / 2)
    height = int(image.getHeight())
    height_center = int(height / 2)
    image = Image(Point(width_center, height_center),file_name_input)
    image_window = GraphWin("Colorless Converter", width, height)
    image.draw(image_window)

    row = 0
    column = 0

    for row in range(height):
        for column in range(width):
            red_value, green_value, blue_value = image.getPixel(column, row)
            gray_set = int(round(0.299 * red_value + 0.587 * green_value + 0.114 * blue_value))
            image.setPixel(column, row, color_rgb(gray_set, gray_set, gray_set))
            image_window.update()
    image.save(file_name_output)
    image_window.getMouse()
    image_window.close()
    return file_name_output

colorless_picture_converter()