python - PIL: Can't vertically align text accurately despite taking font into account -
i want create simple python script let's me create image file , place word dead-center on canvas. however, while can word align horizontally, can't center vertically. on macos btw. have tried far:
import os pil import image, imagedraw, imagefont font_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'fonts') font = imagefont.truetype(os.path.join(font_path, "verdana.ttf"), 80) w, h = (1200,200) msg = "hola" im = image.new("rgb",(w,h),(255,255,255)) draw = imagedraw.draw(im) w, h = draw.textsize(msg, font) draw.text(((w-w)/2,(h-h)/2), msg, font=font, fill="black") im.save("output_script.png", "png")
i considered font in textsize calculation. word still appears 5-10% below middle. ideas?
textsize
seems return size of actual text, not of line. it's smaller ooo
xxx
!
i think should use 80 — size gave pil — height, although can't guarantee it's correct.
Comments
Post a Comment