If you need programmatically to add text on image you can use Pill - image processing library. Use is very simple and intuitive. Automation of images in python is a common task for web developers. All you need is to open an image file, setup the parameters and give simple text. In the example below you can see how we are adding watermark to image (transparent text on the image) and subtitle like text(white text on black background):

from PIL import Image, ImageDraw, ImageFont

img = Image.open("..\\images\\strawberry.jpg")

draw = ImageDraw.Draw(img)
font = ImageFont.truetype(
    "C:\\Windows\\Fonts\\Arial.ttf", 33)
width, height = img.size
x, y = (width-200, height-100)
xx, yy = (width-400, height-100)

text = "Softhints"
w, h = font.getsize(text)

draw.rectangle((x, y, x + w, y + h), fill='black')
draw.text((x, y), text, fill='white', font=font)   # draw white text on black background

draw.text((xx, yy), text, fill=(0, 0, 0, 15), font=font) # draw transparant text

img.show()

Show description of the program:

  • the program is tested on windows but it should be easy to be changed for linux by changing: font = ImageFont.truetype("C:\Windows\Fonts\Arial.ttf", 33)
  • Import required Pill dependencies
from PIL import Image, ImageDraw, ImageFont
  • Open any image on the file system
img = Image.open("..\\images\\strawberry.jpg")
draw = ImageDraw.Draw(img)
  • Chose where to place the text(right bottom corner) and text(also the font and size)
font = ImageFont.truetype(
    "C:\\Windows\\Fonts\\Arial.ttf", 33)
width, height = img.size
x, y = (width-200, height-100)
xx, yy = (width-400, height-100)

text = "Softhints"
w, h = font.getsize(text)
  • Draw transperant text
draw.text((xx, yy), text, fill=(0, 0, 0, 15), font=font) # draw transparant text
  • Draw white text on black background
draw.rectangle((x, y, x + w, y + h), fill='black')
draw.text((x, y), text, fill='white', font=font)   # draw white text on black background
  • Show the result
img.show()