简体   繁体   中英

How to change the pixel color of a PhotoImage python

So i have a program for gui i want to change the pixel color based on the values of the rgb sliders i made, I can't figure out how to change the pixel colors. I searched for like an hour and I can't find anything.

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()

that's what i have so far

EDIT: I did it with a method defined Fill here it is` def fill(self):

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))

and i used that instead of my changeColor method and it works beautifully!

First of all, you have to put something into the image. Since you are iterating over every pixel you probably want to set the color defined by the sliders into every pixel, like so:

image.put("#%02x%02x%02x" % (red, green, blue), (x, y))

The next thing is the definition of your button. The line

self.button = Button(self, text="Change Colors", command=self.changeColor(self._image))

first executes self.changeColor(self._image) and passes the return value as a parameter to the button. As described here you cannot pass parameters to the command method by default but also describes how to circumvent that.

So a possible solution would be something like this:

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()

Since you change the color pixel by pixel this takes some time depending on your source image, you might want to look at ways to set more than one pixel with one call. You can find a way in the PhotoImage page of the tkinter wiki

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM