title: How to find the correct Image size
body: <p>I am trying to convert Text to image in Python</p>
  <p>This is the code:
  As long as the text is in single line,</p>
  <p>For example &quot;Text to write on img 1234567890&quot;</p>
  <p>It is fine
  <a href="https://i.stack.imgur.com/Mm1EB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Mm1EB.png" alt="enter image description here" /></a></p>
  <p>But if the text contains &quot;\n&quot; then the image clips and size calculations become incorrect</p>
  <p>&quot;Text to write on \n img 1234567890&quot;</p>
  <p><a href="https://i.stack.imgur.com/AgsHa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AgsHa.png" alt="enter image description here" /></a></p>
  <p>Please help</p>
  <pre><code>import numpy as np
  import time
  import text_to_image
  from PIL import Image, ImageDraw, ImageFont
  import os
  from win32api import GetSystemMetrics
  
  
  def text_on_img(filename='01.png', text=&quot;Text to write on \n img 1234567890&quot;, size=200, color=(0,0,0), bg='white'):
      &quot;Draw a text on an Image, saves it, show it&quot;
      fnt = ImageFont.truetype('arial.ttf', size)
      # create image
      image = Image.new(mode = &quot;RGB&quot;, size = (int(size/2)*len(text),size+50), color = bg)
      draw = ImageDraw.Draw(image)
      # draw text
      draw.text((10,10), text, font=fnt, fill=(0,0,0))
      # save file
      image.save(filename)
      # show file
      os.system(filename)
  
  
  text_on_img()
  </code></pre>