from PIL import ImageFont,Image, ImageDraw
imagefile = "base.jpg"
im1 = Image.open(imagefile)
draw = ImageDraw.Draw(im1)
draw.text((100,100),"helloworld")
im1.save("res.png")
I want to add a rectangular border in "hello world" ,like that,
CodePudding user response:
For drawing a rectangle around the text you need to know the height and width of the text. Therefore the solution should be:
from PIL import ImageFont,Image, ImageDraw
imagefile = "base.jpg"
im1 = Image.open(imagefile)
draw = ImageDraw.Draw(im1)
font = ImageFont.load_default()
text = "helloworld"
draw.text((100,100),text, font=font)
text_width, text_height = font.getmask(text).size
draw.rectangle(((100, 100), (100 text_width, 100 text_height)), outline=(0,0,255))
im1.save("res.png")