【发布时间】:2021-02-27 06:45:23
【问题描述】:
我仍在尝试弄清楚如何将图像转换为黑白。
我有一个带有命令按钮的 GUI,上面写着“将图像转换为黑白”。我将该命令绑定到def bAndW(self)。它在 GUI 上加载当前图像,但不会转换为黑白。
下面是代码:
def bAndW(self):
from images import Image
LoadAFile = self.inputText.getText()
CurrentImage = open(LoadAFile)
image = Image(LoadAFile)
image.draw()
BlackAndWhite(image)
image.draw()
#self.imageLabel["image"] = self.image
def BlackAndWhite(image):
blackPixel = (0,0,0)
whitePixel = (255,255,255)
for y in range(image.getHeight()):
for x in range(image.getWidth()):
(r, g, b) = image.getPixel(x,y)
average = (r + b + g) /3
if average < 128:
image.setPixel(x,y,blackPixel)
else:
image.setPixel(x,y, whitePixel)
这是错误信息:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "/Users/movio/Desktop/CSCI/P6.1.py", line 50, in bAndW
BlackAndWhite(image)
NameError: name 'BlackAndWhite' is not defined
【问题讨论】:
-
如果
BlackAndWhite()是同一个类中的函数,则应该使用self.BlackAndWhite(image)。也应该使用def BlackAndWhite(self, image)。 -
我相信 PIL 有一个功能可以做到这一点。
标签: python python-3.x python-2.7 tkinter