Created
February 25, 2022 23:27
-
-
Save timrprobocom/d1de6ac955af7003e7abd2ca8e72955c to your computer and use it in GitHub Desktop.
How to draw text in boxes
This file contains 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
from PIL import ImageDraw, Image, ImageFont | |
BLACK = (0,0,0) | |
WHITE = (255,255,255) | |
LTBLUE = (192,192,255) | |
fig = Image.new( "RGB", (320,320), WHITE ) | |
arial = ImageFont.truetype("../.fonts/arial.ttf", 32 ) | |
y = 100 | |
def drawboxstring( img, s, y ): | |
xleft = (fig.width - 50*len(s)) // 2 | |
d = ImageDraw.Draw(img) | |
for c in s: | |
d.rectangle( ((xleft, y), (xleft+50,y+50)), outline=BLACK, fill=LTBLUE ) | |
d.text( (xleft+12, y+10), c, font=arial, fill=BLACK ) | |
xleft += 50 | |
drawboxstring( fig, "AB", y ) | |
y += 50 | |
drawboxstring( fig, "CDE", y ) | |
y += 50 | |
drawboxstring( fig, "FGHI", y ) | |
fig.save( "abc.png" ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the output:
![abc](https://user-images.githubusercontent.com/5384204/155816493-6abc7ded-e357-435b-a85a-e66eafee05d0.png)