Python ImageTK Module



We already know that tkinter is a package used for building Graphical user interface applications in Python.Now ImageTk module is used with graphical interface functions of the Tkinter package.
So far with Pillow library we have loaded the various formats of image,manipulated the image and finally displayed with open and show methods.If it is required to display the image in tkinter window then ImageTk is helpful..
The ImageTk module contains two classes namely PhotoImage and BitmapImage

PhotoImage class
Creates photo image that is compatible with Tkinter which can be used everywhere Tkinter expects an image object.

methods
height()

Get the height of the image.
width()

Get the width of the image.



BitmapImage class
Creates photo image that is compatible with Tkinter which can be used everywhere Tkinter expects an image object.
methods
height()

Get the height of the image.
width()

Get the width of the image.
paste(im, box=None)

Paste a PIL image into the photo image

Let us consider the below example for simply displaying the Tkinter window.

import Tkinter
top = Tkinter.Tk()
# Code to add widgets will go here...
top.mainloop()

Now let us include the ImageTk module to display the jpg image in the window.

import Tkinter
from PIL import ImageColor
from PIL import ImageTk
im = Image.open('flower1.jpg') 
top = Tkinter.Tk()
# Convert the Image object into a TkPhoto object
pimage_obj = ImageTk.PhotoImage(im)
Tkinter.Label(top, image=pimage_obj).pack() 
# Start the GUI
top.mainloop()

Output:
scenry

Advertisements