Creating Blank Images in Python (allowing pixel by pixel manipulation)
13,300
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.
Related videos on Youtube
Author by
rectangletangle
Updated on May 18, 2022Comments
-
rectangletangle about 2 years
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?
-
Nate about 11 yearsActually here is where Image.new(...) is described: pythonware.com/library/pil/handbook/image.htm