Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import random
IMG_HEIGHT = 512
IMG_WIDTH = 512
paths = filter_fonts_get_paths(df, root=ROOT, subsets=[''], variants=['bold'], category='')
r = random.randrange(0, len(paths))
# sample text and font
text = "G"
text_size = 400
x = IMG_WIDTH/2
y = IMG_HEIGHT*3/4
font = ImageFont.truetype(paths[r], text_size)
# get text info (not being used but may be useful)
text_width, text_height = font.getsize(text)
left, top, right, bottom = font.getbbox(text)
print('text w & h: ', text_width, text_height)
print(left, top, right, bottom)
# create a blank canvas with extra space between lines
canvas = Image.new('RGB', (IMG_WIDTH, IMG_HEIGHT), "black")
# draw the text onto the text canvas
draw = ImageDraw.Draw(canvas)
draw.text((x, y), text, 'white', font, anchor='ms')
plt.imshow(canvas)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment