You can get the code and the images here:Ĭheck for the file called image_slideshow. ![]() To import ImageTk and Image in a Python console, enter: from PIL import ImageTk, Image. Self.label = tk.Label(self.root, image=self.img) How to Manipulate Images with PIL and ImageTk. With some little changes we can, after this example, see how to make it actually change at a certain pace. Well, itâs not exactly a slideshow, because you have to manually click on a button to make the images to change. Letâs add some code to make a sort of slideshow. This is the image in the window Letâs make a slideshow ![]() Window(image) If you do not want to use classes, this is some alternative code for you. Label = tk.Label(self.root, image=self.img) ![]() GIF then load them (Far easier, but as jonrsharpe said, nothing will work without converting the file first) or you can port your program to Python 2.7 and use the Python Imaging Library (PIL) and its tkinter extensions to use a PNG image. You will either need to convert the files to. Here is an example of how you can use the PhotoImage class to display an image in a tkinter window: import tkinter as tk tkinter only supports 3 file formats off the bat which are GIF, PGM, and PPM. This class allows you to load an image from a file and display it in a tkinter GUI. # go from pil color (r, g, b) to tk color format "#rrggbb"Ĭolor_pil = (rn.randrange(256), rn.randrange(256), rn.randrange(256))Ĭolor_tk = '#' + "".join("%02x" % c for c in color_pil)Ĭv_tk.create_oval(x, y, x+r, y+r, fill=color_tk)Ĭv_pil.To show an image in tkinter, you will need to use the PhotoImage class from the tkinter.PhotoImage module. # create a random color for PIL and Tkinter Img_pil = Image.new("RGB", (w, h), black) # create a PIL canvas in memory and use in parallel bmp filesĬv_tk = tk.Canvas(width=w, height=h, bg='black') Root.title("Idle for 5 seconds, save to file circles.png") """freeze the action for 5 seconds and save to file""" # modified vegaseat's code from (circles): # draws in memory, but can be saved in many formats # the Tkinter canvas can only be saved in postscript format # save the canvas drawing to an image file # a left mouse double click will idle action for 5 seconds and You can save the in memory PIL image in many file formats. """freeze the action for 5 seconds and save image file"""Ĭv.postscript(file="circles.eps") # save canvas as encapsulated postscriptĬhild = SP.Popen("mogrify -format jpg circles.eps", shell=True) # convert eps to jpg with ImageMagickĪnother way is to run Tkinter and PIL simultaneously. I was able to do it by saving the canvas as encapsulated postscript and then using ImageMagick to convert to jpg (this code was run in linux) def idle_5sec(event=None): What I would like to do is to save the double clicked canvas to an image file like. # bind left mouse double click, idle for 5 seconds ![]() # endless loop to draw the random circlesĬolor = '#' + "".join("%02x"%rn.randrange(256) for x in range(3))Ĭv.create_oval(x, y, x+r, y+r, fill=color) Root.title("Happy Circles (click on window to idle for 5 seconds)")Ĭv = tk.Canvas(width=w, height=h, bg='black') # a left mouse double click will idle action for 5 seconds I was exploring random art and modified this nice example from vegaseat: # random circles in Tkinter
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |