当我尝试:
import Image
import ImageDraw
import ImageFont
def draw_text_center(im, draw, text, font, **kwargs):
text_size = draw.textsize(text, font)
return draw.text(
((im.size[0] - text_size[0]) / 2, (im.size[1] - text_size[1]) / 2),
text, font=font, **kwargs)
im = Image.new('L', (64, 64), 0xFF)
draw = ImageDraw.Draw(im)
draw_text_center(
im, draw, "Hello",
ImageFont.truetype(os.path.expandvars("%SystemRoot%/Fonts/segoeui.ttf"), 16))
del draw
im.show()
我沮丧地取回了非垂直居中的文本:
我究竟做错了什么?如何正确居中文本?