title: Pillow, how to put the text in the center of the image
body: <p>I use the Pillow (PIL) 6.0 and add text in the image. And I want to put the text in the center of the image. Here is my code,</p>
  
  <pre><code>import os
  import string
  from PIL import Image
  from PIL import ImageFont, ImageDraw, ImageOps
  
  width, height = 100, 100
  
  text = 'H'
  font_size = 100
  
  os.makedirs('./{}'.format(text), exist_ok=True)
  
  img = Image.new("L", (width, height), color=0)   # "L": (8-bit pixels, black and white)
  font = ImageFont.truetype("arial.ttf", font_size)
  draw = ImageDraw.Draw(img)
  w, h = draw.textsize(text, font=font)
  draw.text(((width-w)/2, (height-h)/2), text=text, fill='white', font=font)
  
  img.save('H.png')
  </code></pre>
  
  <h1>Here is the output:</h1>
  
  <p><a href="https://i.stack.imgur.com/i51jU.png" rel="noreferrer"><img src="https://i.stack.imgur.com/i51jU.png" alt="enter image description here"></a></p>
  
  <h1>Question:</h1>
  
  <p>The text is in the center horizontally, but not in the center vertically. How can I put it in the center horizontally and vertically?</p>