[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky] [Facebook]
[Build Your Own Python Action Arcade!]

[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: Make and save checkerboard images in Python

[This program lets you make and save checkerboard images in Python]

Sometimes it's useful to make a checkerboard image. For example, these posts display transparent images on top of a checkboard so you can see where the image is transparent.

This example describes a function you can use to easily generate checkerboard images with rectangles of different sizes and colors.

Drawing Checkerboards

The following make_checkboard function creates and returns a checkerboard image.

from PIL import Image, ImageDraw def make_checkboard(wid, hgt, dx, dy, color1, color2): '''Return a checkerboard image.''' # Make the image, initially filled with color1. image = Image.new('RGBA', (wid, hgt), color1) # Add color2 rectangles. dr = ImageDraw.Draw(image) for c, x in enumerate(range(0, wid, dx)): for r, y in enumerate(range(0, hgt, dy)): if (c + r) % 2 == 1: rect = (x, y, x + dx - 1, y + dy - 1) dr.rectangle(rect, fill=color2) return image

The code starts by creating a new image of the desired size. Lately I've been using these checkerboards to show transparency, so this function creates the image in RGBA format so it includes the alpha (aka opacity) channel in addition to red, green, and blue color components. Using the RGBA format makes it easier to composite the checkerboard with the transparent image.

Initially the image is filled with the first color color1. (Blue in the picture at the top of this post.)

Next, the code creates an ImageDraw object to draw rectangles on the image.

The function then loops through the checkerboard's rows and columns. Inside the loop, r and c give the row and column numbers for the rectangles and (x, y) gives the coordinates of the squares' upper left corners.

Consider the statement for i, x in enumerate(range(0, wid, dx)). The range statement makes variable x start at 0 and loop until it reaches the width of the desired image wid. Between iterations x increases by dx so the x values produced are the coordinates of the rectangles' upper left corners. The y loop works similarly.

Inside the loop, the code checks (c + r) % 2 and, if that value is 1, it colors the current rectangle with color2. The value (c + r) % 2 is 1 for every other rectangle and produces the checkerboard pattern.

When the loops end, the function simply returns the image.

[A short, wide checkerboard with non-square rectangles]

Conclusion

That's all there is to it. The program lets you change the image's size, the rectangles' sizes, and the colors. Download it to experiment generate checkerboards and to see additional details like how the main program displays checkerboards and saves them into files.
© 2025 - 2026 Rocky Mountain Computer Consulting, Inc. All rights reserved.