因此,我的任务是使用链接(http://mcsp.wartburg.edu/zelle/python/)中提供的图形库将我选择的图像从彩色转换为灰度。我在StackOverFlow(Python 3: Converting image to grayscale)上发现了另一个帖子,它为我提供了一个基础,但是我很遗憾没能解决所有的问题,需要第二双眼睛来帮助我。我也不确定如何提示在屏幕上弹出灰度图像,这样用户就可以知道保存文件的样子。
截至目前,该程序运行平稳,因为它在没有打嗝的情况下贯穿整个代码,但它不会保存灰度图像。
如果此代码看起来不整齐,我还想提前抱歉。我是编程和在论坛上发帖的新手,所以我提前道歉。感谢您的帮助,因为它非常需要并非常感谢。
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()
答案 0 :(得分:0)
我也一直在使用Zelles图形来进行灰度图像处理。我能够通过在for循环中切换宽度和高度并减少image.save(file_name_output),image_window close()上的缩进并返回file_name_output来使代码工作,如下所示...(我还添加了一个image_window .getMouse()方法在转换为灰度后保持窗口打开。)注意:我使用的是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()