[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky]
[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Use PIL to draw a transparent image over a checkerboard in Python

[An image with transparent parts drawn in top of a checkerboard with PIL in Python]

This example draws an image over a checkerboard so you can see where the image is transparent.

draw_over_checkerboard

The key is the following draw_over_checkerboard function.

from PIL import Image, ImageDraw import math def draw_over_checkerboard(image, checker_wid=20, color0='black', color1='yellow'): '''Draw this image drawn on top of a checkered background.''' # Make an image to draw on. wid = image.width hgt = image.height checkerboard = Image.new('RGBA', (wid, hgt), color0) dr = ImageDraw.Draw(checkerboard) # Draw the checkerboard. num_cols = math.ceil(wid / checker_wid) num_rows = math.ceil(hgt / checker_wid) for r in range(num_rows): for c in range(num_cols): if (r + c) % 2 == 1: rect = [c * checker_wid, r * checker_wid, (c + 1) * checker_wid, (r + 1) * checker_wid] dr.rectangle(rect, fill=color1, outline=None) # Draw the image on top. checkerboard.paste(image, mask=image) return checkerboard

The function first gets the input image's dimensions and then makes a new image of that size. It fills the new image with the color passed in via the color0 parameter. It then creates an ImageDraw object to draw on the object.

The code then loops through the necessary number of rows and columns. If the row number plus the column number is an odd number, the function draws a rectangle filled with color1 at that row and column. The result is a checkerboard.

The code then pastes the image on top of the checkerboard using the image itself as a mask. For an RGBA image, the paste method uses the mask's alpha channel. In this case, that means the original image's alpha channel is used to set the image's transparency.

The alpha channel gives a pixel's opacity. If alpha is 22, the pixel is copied as it is. If alpha is 0, the pixel is not drawn (or you can think of it as drawn transparently) and the background checkerboard shows through. For values between 0 and 255, the pixel is partially copied. For example, if alpha is 255 * 0.8 = 204, the pixel is copied 80%.

After it pastes the image onto the checkerboard, the function returns the resulting composite image.

Main Program

The following code shows the example's main program.

class CheckerboardApp: def __init__(self): self.window = tk.Tk() self.window.title('transparent_over_checkerboard') self.window.protocol('WM_DELETE_WINDOW', self.kill_callback) # Load the image. image = Image.open('cake.png') # Draw the image over a checkerboard. image = draw_over_checkerboard(image) # Display the result. self.image = ImageTk.PhotoImage(image) label = tk.Label(self.window, image=self.image) label.pack() # Display the window. self.window.focus_force() self.window.mainloop() def kill_callback(self): # Destroy the tkinter window. self.window.destroy()

This code creates the tkinter main window. It then opens the file cake.png, which has some transparent pixels.

The code calls draw_over_checkerboard, converts the result into an ImageTk.PhotoImage, and displays it in a Label widget.

Remember that the PhotoImage must be stored in a safe piece of memory so it isn't garbage collected. This program saves it in self.image so we know it's safe until the program ends.

Conclusion

The draw_over_checkerboard function makes it easy to draw a transparent image on top of a checkerboard so you can see which pixels are transparent. I'll use it in my next post, which removes backgrounds from images.

Download the example to experiment with it and to see additional details.

© 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.