From 0274b9d4a5c8b92a8b0556ae9df43a406670af9c Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 5 Sep 2021 01:35:46 -0700 Subject: [PATCH] Move checkerboard_image to imagetools. --- voussoirkit/imagetools.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/voussoirkit/imagetools.py b/voussoirkit/imagetools.py index 3ef96e8..6280140 100644 --- a/voussoirkit/imagetools.py +++ b/voussoirkit/imagetools.py @@ -1,11 +1,36 @@ import copy import PIL.ExifTags +import PIL.Image ORIENTATION_KEY = None for (ORIENTATION_KEY, val) in PIL.ExifTags.TAGS.items(): if val == 'Orientation': break +def checkerboard_image(color_1, color_2, image_size, checker_size) -> PIL.Image: + ''' + Generate a PIL Image with a checkerboard pattern. + + color_1: + The color starting in the top left. Either RGB tuple or a string + that PIL understands. + color_2: + The alternate color + image_size: + Tuple of two integers, the image size in pixels. + checker_size: + Tuple of two integers, the size of each checker in pixels. + ''' + image = PIL.Image.new('RGB', image_size, color_1) + checker = PIL.Image.new('RGB', (checker_size, checker_size), color_2) + offset = True + for y in range(0, image_size[1], checker_size): + for x in range(0, image_size[0], checker_size * 2): + x += offset * checker_size + image.paste(checker, (x, y)) + offset = not offset + return image + def fit_into_bounds( image_width, image_height,