Created
April 4, 2019 10:25
-
-
Save rrader/c7d340dde84fd74f41b3cbebffe47c75 to your computer and use it in GitHub Desktop.
TextToParrots
This file contains hidden or 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
| # https://stackoverflow.com/questions/36384353/generate-pixel-matrices-from-characters-in-string | |
| # FONT: https://fontstruct.com/fontstructions/show/1404190/cg-pixel-4x5-1 | |
| from PIL import Image | |
| from PIL import ImageFont | |
| from PIL import ImageDraw | |
| import numpy as np | |
| def char_to_pixels(text, path, fontsize): | |
| font = ImageFont.truetype(path, fontsize) | |
| w, h = font.getsize(text) | |
| h *= 2 | |
| image = Image.new('L', (w, h), 1) | |
| draw = ImageDraw.Draw(image) | |
| draw.text((0, 0), text, font=font) | |
| arr = np.asarray(image) | |
| arr = np.where(arr, 0, 1) | |
| arr = arr[(arr != 0).any(axis=1)] | |
| return arr | |
| def display(arr, front='#', back=' '): | |
| result = np.where(arr, front, back) | |
| return [''.join(row) for row in result] | |
| input_str = 'PYTHON' | |
| fontsize = 5 | |
| chars = [] | |
| for c in input_str: | |
| arr = char_to_pixels( | |
| c, | |
| 'cg-pixel-3x5-mono.ttf', | |
| fontsize=fontsize) | |
| chars.append(display(arr, back=':shuffle_parrot:', front=':catdance:')) | |
| print('\n'.join(''.join(row) for row in zip(*chars))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment