如何更改PhotoImage python的像素颜色

时间:2015-04-17 14:20:40

标签: python tkinter

所以我有一个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()

到目前为止我所拥有的

编辑:我用定义的方法做到这里填写它是` 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方法,它工作得很漂亮!

1 个答案:

答案 0 :(得分:1)

首先,你必须在图像中添加一些东西。由于您在迭代每个像素,您可能希望将滑块定义的颜色设置为每个像素,如下所示:

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)并将返回值作为参数传递给按钮。如前所述,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

中找到一种方法