如何更改 PhotoImage 的像素颜色 python
How to change the pixel color of a PhotoImage python
所以我有一个 gui 程序,我想根据我制作的 rgb 滑块的值更改像素颜色,但我不知道如何更改像素颜色。
我搜索了将近一个小时,但我找不到任何东西。
from tkinter import *
class Colors(Frame):
def __init__(self):
Frame.__init__(self)
self._image = PhotoImage(file="r.gif")
self._imageLabel = Label(self, image=self._image)
self._imageLabel.grid()
self.master.title("Color Changer")
self.grid()
self.red = Scale(self, from_=0, to=255, label="Red", fg="red", )
self.red.grid(row=0, column=1)
self.green = Scale(self, from_=0, to=255, label="Green", fg='green')
self.green.grid(row=0, column=2)
self.blue = Scale(self, from_=0, to=255, label="Blue", fg="blue")
self.blue.grid(row=0, column=3)
self.button = Button(self, text="Change Colors", command=self.changeColor(self._image))
self.button.grid(row=1, column=2)
def changeColor(self, image):
red = self.red.get()
blue = self.blue.get()
green = self.green.get()
for y in range(image.height()):
for x in range(image.width()):
image.put()
def main():
Colors().mainloop()
main()
这就是我目前所拥有的
编辑:我用一个定义的方法完成了这里的 Fill it is`
def 填充(自我):
def fill(self):
"""Fill image with a color=(r,b,g)."""
r, g, b = (self.red.get(), self.green.get(), self.blue.get())
width = self._image.width()
height = self._image.height()
hexcode = "#%02x%02x%02x" % (r, g, b)
horizontal_line = "{" + " ".join([hexcode] * width) + "}"
self._image.put(" ".join([horizontal_line] * height))
我用它代替了我的 changeColor 方法,而且效果很好!
首先,你必须在图像中加入一些东西。由于您要遍历每个像素,因此您可能希望将滑块定义的颜色设置到每个像素中,如下所示:
image.put("#%02x%02x%02x" % (red, green, blue), (x, y))
接下来是按钮的定义。行
self.button = Button(self, text="Change Colors", command=self.changeColor(self._image))
首先执行self.changeColor(self._image)
并将return值作为参数传递给按钮。正如所描述的那样 here 默认情况下您不能将参数传递给命令方法,但也描述了如何避免这种情况。
所以一个可能的解决方案是这样的:
from tkinter import *
class Colors(Frame):
def __init__(self):
Frame.__init__(self)
self._image = PhotoImage(file="r.gif")
self._imageLabel = Label(self, image=self._image)
self._imageLabel.grid()
self.master.title("Color Changer")
self.grid()
self.red = Scale(self, from_=0, to=255, label="Red", fg="red", )
self.red.grid(row=0, column=1)
self.green = Scale(self, from_=0, to=255, label="Green", fg='green')
self.green.grid(row=0, column=2)
self.blue = Scale(self, from_=0, to=255, label="Blue", fg="blue")
self.blue.grid(row=0, column=3)
self.button = Button(self, text="Change Colors", command=self.changeColor)
self.button.grid(row=1, column=2)
def changeColor(self):
red = self.red.get()
blue = self.blue.get()
green = self.green.get()
for y in range(self._image.height()):
for x in range(self._image.width()):
self._image.put("#%02x%02x%02x" % (red,green,blue), (x, y))
def main():
Colors().mainloop()
main()
由于您是逐个像素地更改颜色,因此这需要一些时间,具体取决于您的源图像,您可能想了解一下通过一次调用设置多个像素的方法。您可以在 PhotoImage page of the tkinter wiki
中找到方法
所以我有一个 gui 程序,我想根据我制作的 rgb 滑块的值更改像素颜色,但我不知道如何更改像素颜色。 我搜索了将近一个小时,但我找不到任何东西。
from tkinter import *
class Colors(Frame):
def __init__(self):
Frame.__init__(self)
self._image = PhotoImage(file="r.gif")
self._imageLabel = Label(self, image=self._image)
self._imageLabel.grid()
self.master.title("Color Changer")
self.grid()
self.red = Scale(self, from_=0, to=255, label="Red", fg="red", )
self.red.grid(row=0, column=1)
self.green = Scale(self, from_=0, to=255, label="Green", fg='green')
self.green.grid(row=0, column=2)
self.blue = Scale(self, from_=0, to=255, label="Blue", fg="blue")
self.blue.grid(row=0, column=3)
self.button = Button(self, text="Change Colors", command=self.changeColor(self._image))
self.button.grid(row=1, column=2)
def changeColor(self, image):
red = self.red.get()
blue = self.blue.get()
green = self.green.get()
for y in range(image.height()):
for x in range(image.width()):
image.put()
def main():
Colors().mainloop()
main()
这就是我目前所拥有的
编辑:我用一个定义的方法完成了这里的 Fill it is` def 填充(自我):
def fill(self):
"""Fill image with a color=(r,b,g)."""
r, g, b = (self.red.get(), self.green.get(), self.blue.get())
width = self._image.width()
height = self._image.height()
hexcode = "#%02x%02x%02x" % (r, g, b)
horizontal_line = "{" + " ".join([hexcode] * width) + "}"
self._image.put(" ".join([horizontal_line] * height))
我用它代替了我的 changeColor 方法,而且效果很好!
首先,你必须在图像中加入一些东西。由于您要遍历每个像素,因此您可能希望将滑块定义的颜色设置到每个像素中,如下所示:
image.put("#%02x%02x%02x" % (red, green, blue), (x, y))
接下来是按钮的定义。行
self.button = Button(self, text="Change Colors", command=self.changeColor(self._image))
首先执行self.changeColor(self._image)
并将return值作为参数传递给按钮。正如所描述的那样 here 默认情况下您不能将参数传递给命令方法,但也描述了如何避免这种情况。
所以一个可能的解决方案是这样的:
from tkinter import *
class Colors(Frame):
def __init__(self):
Frame.__init__(self)
self._image = PhotoImage(file="r.gif")
self._imageLabel = Label(self, image=self._image)
self._imageLabel.grid()
self.master.title("Color Changer")
self.grid()
self.red = Scale(self, from_=0, to=255, label="Red", fg="red", )
self.red.grid(row=0, column=1)
self.green = Scale(self, from_=0, to=255, label="Green", fg='green')
self.green.grid(row=0, column=2)
self.blue = Scale(self, from_=0, to=255, label="Blue", fg="blue")
self.blue.grid(row=0, column=3)
self.button = Button(self, text="Change Colors", command=self.changeColor)
self.button.grid(row=1, column=2)
def changeColor(self):
red = self.red.get()
blue = self.blue.get()
green = self.green.get()
for y in range(self._image.height()):
for x in range(self._image.width()):
self._image.put("#%02x%02x%02x" % (red,green,blue), (x, y))
def main():
Colors().mainloop()
main()
由于您是逐个像素地更改颜色,因此这需要一些时间,具体取决于您的源图像,您可能想了解一下通过一次调用设置多个像素的方法。您可以在 PhotoImage page of the tkinter wiki
中找到方法