Python Pillow with Tkinter BitmapImage and PhotoImage objects


Python Pillow with Tkinter BitmapImage and PhotoImage objects



”;


The ImageTk module in the python pillow library provides functionality to create and manipulate Tkinter BitmapImage and PhotoImage objects from PIL images. This tutorial discusses the descriptions of the key classes and methods within the ImageTk module.

These classes and methods provide convenient ways to work with Tkinter images using PIL in Python, allowing for integration of images into graphical user interfaces.

Tkinter BitmapImage class in Python Pillow

The ImageTk.BitmapImage class represents a Tkinter-compatible bitmap image that can be utilized wherever Tkinter expects an image object.

Here are the key details about the class −

  • The provided image must have a mode of “1”.

  • Pixels with a value of 0 are considered transparent.

  • Additional options, if provided, are forwarded to Tkinter.

  • A commonly used option is foreground, allowing specification of the color for non-transparent parts.

Following is the syntax of the ImageTk.BitmapImage() class −


class PIL.ImageTk.BitmapImage(image=None, **kw)

Where,

  • image − A PIL image with mode “1”. Pixels with value 0 are treated as transparent.

  • **kw − Additional options passed on to Tkinter. The most commonly used option is foreground, which specifies the color for the non-transparent parts.

Following are the list of methods provided by the BitmapImage class −

  • height() − Returns the height of the image in pixels.

  • width() − Returns the width of the image in pixels.

Example

Here is an example demonstrating creating Tkinter windows, loading images with PIL, and then using ImageTk.BitmapImage to create Tkinter-compatible image objects for display in a Tkinter Label.


from tkinter import *
from PIL import ImageTk, Image

# Create a Tkinter window
root = Tk()

# Create a sample image 1-bit mode image (black and white)
pil_image = Image.new("1", (700, 300), color=1)  
 
# Create a BitmapImage from the PIL image
bitmap_image = ImageTk.BitmapImage(pil_image, background='''', foreground=''gray'')

# Display the BitmapImage in a Tkinter Label
label = Label(root, image=bitmap_image)
label.pack()

# Run the Tkinter event loop
root.mainloop()

Output


tk bitmapimage

The PhotoImage class in Python Pillow

The ImageTk.PhotoImage() class represents a Tkinter-compatible photo image in Pillow, it suitable for use wherever Tkinter expects an image object.

Here are the key details about the class −

  • If the image is in RGBA format, pixels with an alpha value of 0 are treated as transparent.

  • The constructor can be initialized with either a PIL image, or a mode and size. Alternatively, you have the option to use the file or data parameters to initialize the photo image object.

Here is the syntax of the ImageTk.PhotoImage() class −


class PIL.ImageTk.PhotoImage(image=None, size=None, **kw)

Where,

  • image − Represents either a PIL image or a mode string. If a mode string is used, a size must also be given.

  • size − If the first argument is a mode string, this defines the size of the image.

  • file − A filename to load the image from using Image.open(file).

  • data − An 8-bit string containing image data, as loaded from an image file.

Following are the list of methods provided by the PhotoImage() class −

  • height() − Get the height of the image in pixels.

  • width() − Get the width of the image in pixels.

  • paste(im) − Paste a PIL image into the photo image. Note that this can be slow if the photo image is displayed.

  • Parameters − im – A PIL image. The size must match the target region. If the mode does not match, the image is converted to the mode of the bitmap image.

Example

Here is an example demonstrating creating Tkinter windows, loading images with PIL, and then using ImageTk.PhotoImage to create Tkinter-compatible image objects for display in a Tkinter Label.


from tkinter import *
from PIL import ImageTk, Image

# Create a Tkinter window
root = Tk()

# Create a canvas widget
canvas = Canvas(root, width=700, height=400, bg=''white'')
canvas.grid(row=2, column=3)

# Load an image using PIL and create a PhotoImage
pil_image = Image.open("Images/pillow-logo-w.jpg")
tk_image = ImageTk.PhotoImage(pil_image)

# Draw the image on the canvas
canvas.create_image(20, 20, anchor=NW, image=tk_image)

# Start the Tkinter event loop
mainloop()

Output


pillow logo w

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *