diff options
author | AL-LCL <alvin@alvinhavel.com> | 2023-05-19 11:01:49 +0200 |
---|---|---|
committer | AL-LCL <alvin@alvinhavel.com> | 2023-05-19 11:01:49 +0200 |
commit | 20dbeb2f38684c65ff0a4b99012c161295708e88 (patch) | |
tree | a5b8445f55da2fbbb92443b68e9d7354a290c598 /domestic/utility/text_to_image.py |
Diffstat (limited to 'domestic/utility/text_to_image.py')
-rw-r--r-- | domestic/utility/text_to_image.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/domestic/utility/text_to_image.py b/domestic/utility/text_to_image.py new file mode 100644 index 0000000..0adb304 --- /dev/null +++ b/domestic/utility/text_to_image.py @@ -0,0 +1,42 @@ +from PIL import ImageFont, Image, ImageDraw, ImageOps + +from domestic.utility.write_error import * + + +def text_to_image(text): + PIXEL_ON = 255 + PIXEL_OFF = 1 + + grayscale = 'L' + lines = tuple(l.rstrip() for l in text.split('\n')) + + large_font = 40 + font_path = 'cour.ttf' + + try: + font = ImageFont.truetype(font_path, size=large_font) + except IOError as err: + write_error(err) + font = ImageFont.load_default() + + pt2px = lambda pt: int(round(pt * 96.0 / 72)) + max_width_line = max(lines, key=lambda s: font.getsize(s)[0]) + test_string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + max_height = pt2px(font.getsize(test_string)[1]) + max_width = pt2px(font.getsize(max_width_line)[0]) + height = max_height * len(lines) + width = int(round(max_width + 40)) + image = Image.new(grayscale, (width, height), color=PIXEL_OFF) + draw = ImageDraw.Draw(image) + + vertical_position = 5 + horizontal_position = 5 + line_spacing = int(round(max_height * 0.8)) + + for line in lines: + draw.text((horizontal_position, vertical_position), line, fill=PIXEL_ON, font=font) + vertical_position += line_spacing + c_box = ImageOps.invert(image).getbbox() + image = image.crop(c_box) + + return image
\ No newline at end of file |