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_ascii.py |
Diffstat (limited to 'domestic/utility/text_to_ascii.py')
-rw-r--r-- | domestic/utility/text_to_ascii.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/domestic/utility/text_to_ascii.py b/domestic/utility/text_to_ascii.py new file mode 100644 index 0000000..0b6d634 --- /dev/null +++ b/domestic/utility/text_to_ascii.py @@ -0,0 +1,17 @@ +import numpy + +from PIL import Image, ImageDraw, ImageFont + + +def text_to_ascii(text): + myfont = ImageFont.truetype('arial.ttf', 18) + size = myfont.getsize(text) + img = Image.new('1', size, 'black') + draw = ImageDraw.Draw(img) + draw.text((0, 0), text, 'white', font=myfont) + pixels = numpy.array(img, dtype=numpy.uint8) + chars = numpy.array([' ', '%'], dtype='U1')[pixels] + strings = [line for line in chars.view('U' + str(chars.shape[1])).flatten() if not line.isspace()] + result = '\n'.join(strings) + + return result
\ No newline at end of file |