0

Is there the most popular way of adding a text on an image in Python? I found a few completely different approaches, this seems to be best, but it doesn't work:

from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw 
img = Image.open("/full_path/1.jpg")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-C.ttf", 16)
draw.text((0, 0),"Sample Text",(255,255,255),font=font)
img.save('/full_path/sample-out.jpg')

After its running, the picture still doesn't have a text on it.

1
  • 1
    Try putting the text somewhere else. IIRC, the PIL coordinate system puts (0,0) at the top left. If draw.text() takes a coordinate for the baseline of the text, you're drawing outside the image. Commented Aug 18, 2013 at 3:36

1 Answer 1

3

try this:

import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

img=Image.open("pathToImage")

font = ImageFont.truetype("pathToFont",10)

draw = ImageDraw.Draw(img)
draw.text((0, 0),"This is a test",(255,255,0),font=font)
draw = ImageDraw.Draw(img)
draw = ImageDraw.Draw(img)
img.save("a_test.png")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.