Skip to content

Instantly share code, notes, and snippets.

@erraticgenerator
Last active November 1, 2020 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erraticgenerator/6b0093720bcfb41d76b523abd7965815 to your computer and use it in GitHub Desktop.
Save erraticgenerator/6b0093720bcfb41d76b523abd7965815 to your computer and use it in GitHub Desktop.
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