Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Blank Images in Python (allowing pixel by pixel manipulation)

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?

like image 300
rectangletangle Avatar asked Jan 31 '11 03:01

rectangletangle


1 Answers

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.

like image 183
Paul Avatar answered Sep 29 '22 12:09

Paul