Last active
April 30, 2022 19:54
-
-
Save victorfei/1843ffd5fe871ef74d6bb3ce2a01dee8 to your computer and use it in GitHub Desktop.
Draw a chessboard image using python PIL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# adapted from: | |
# http://wordaligned.org/articles/drawing-chessboards | |
from PIL import Image, ImageDraw | |
from itertools import cycle | |
def draw_chessboard(n=8, pixel_width=500): | |
""" | |
Draw an n x n chessboard using PIL. | |
""" | |
def sq_start(i): | |
""" | |
Return the square corners, suitable for use in PIL drawings | |
""" | |
return i*pixel_width / n | |
def square(i, j): | |
""" | |
Return the square corners, suitable for use in PIL drawing | |
""" | |
return map(sq_start, [i, j, i+1, j+1]) | |
image = Image.new("L", (pixel_width, pixel_width)) | |
draw_square = ImageDraw.Draw(image).rectangle | |
squares = (square(i,j) | |
for i_start, j in zip(cycle((0, 1)), range(n)) | |
for i in range(i_start, n, 2)) | |
for sq in squares: | |
draw_square(sq, fill='white') | |
image.save("chessboard.png") | |
# draw 8 x 8 chess board with 500 pixel as pixel width | |
draw_chessboard(8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment