7

I have this code here that creates a Tkinter Canvas widget, then embeds an image within it.

import Tkinter
from PIL import ImageTk, Image


class image_manip(Tkinter.Tk):

    def __init__(self):
        Tkinter.Tk.__init__(self)

        self.configure(bg='red')

        self.ImbImage = Tkinter.Canvas(self, highlightthickness=0, bd=0, bg='blue')
        self.ImbImage.pack()

        self.i = ImageTk.PhotoImage(Image.open(r'test.png'))
        self.ImbImage.create_image(150, 100, image=self.i)


def run():
    image_manip().mainloop()
if __name__ == "__main__":
    run()

I'd like to be able to create a blank image within the Canvas widget, so that I could do pixel by pixel manipulation within the widget. How would one go about this?

mpenkov
  • 21,621
  • 10
  • 84
  • 126
rectangletangle
  • 50,393
  • 94
  • 205
  • 275

1 Answers1

13

To create a new blank image (rather than opening one), you can use the Image.new(...) method in place of your Image.open(...). It is described in The Image Module.

Then call self.i.put(...) to do pixel-by-pixel manipulation. (i is the PhotoImage object as in your example.)

Here's some general information on The Tkinter PhotoImage Class.

martineau
  • 119,623
  • 25
  • 170
  • 301
Paul
  • 42,322
  • 15
  • 106
  • 123
  • Actually here is where Image.new(...) is described: http://www.pythonware.com/library/pil/handbook/image.htm – Nate Apr 17 '13 at 23:33