0
我是编程新手,最近开始使用python进行编码。 我正在编写一个带有示例程序代码的教科书。下面的代码来自该书,并且根据它们的亮度(它们的RGB值的总和),通过将颜色分配给每个像素黑色或白色,将彩色图像变成黑白图像。Python tkinter代码不起作用
from tkinter import *
def black_white():
average = 382.5
for x in range (image.width()):
for y in range (image.height()):
c = image.get(x, y)
brightness = int(c[0]) + int(c[1]) + int(c[2])
if brightness < average:
image.put("black", (x))
else:
image.put("white", (x))
window = Tk()
image = PhotoImage(file="1.gif")
button = Button(master=window, command=black_white,
font=("Arial", 14),
text="Bearbeiten")
label = Label(master=window, image=image)
label.pack()
button.pack(fill=X)
window.mainloop()
然而,该代码不起作用,该错误消息我得到的是:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1550, in __call__
return self.func(*args)
File "/Users/(anonymous)/Desktop/programmieren/raspberry_lehrbuch/schwarzweiß.pyw", line 8, in black_white
brightness = int(c[0]) + int(c[1]) + int(c[2])
ValueError: invalid literal for int() with base 10: ' '
我已经做了一些调查,但无法找到任何东西,使它工作。帮助是非常赞赏:) 哦,我使用的MacBook Pro 2010年埃尔卡皮坦
错误表示您尝试将空字符串转换为整数。尝试'int(“”)',你会得到相同的错误信息。在你执行'brightness = int(c [0])+ int(c [1])+ int(c [2])之前检查'c'' – furas