Bin Chen Bin Chen - 1 year ago 93
Python Question

python: any tutorial to overlay text on a picture?

I just wanted to write some text on picture(possible with some simple effects like shadow), I know PIL can do this but is there any tutorial on this?

wim wim
Answer Source

First install Python Imaging Library (pip install Pillow)

Note: you may need to change the path to your font file font_fname.

import scipy as sp
import PIL
import PIL.Image as Image
import PIL.ImageDraw as ImageDraw
import PIL.ImageFont as ImageFont

font_fname = '/usr/share/fonts/truetype/freefont/FreeSansBold.ttf'
font_size = 200
font = ImageFont.truetype(font_fname, font_size)

h, w = 1080, 1920
bg_colour = (255, 255, 255)
bg_image =,w,3), dtype='uint8'), sp.diag(sp.asarray((bg_colour), dtype='uint8')))
image0 = Image.fromarray(bg_image)
draw = ImageDraw.Draw(image0)
draw.text((530, 160), "hello world", font=font, fill='rgb(0, 0, 0)')'hello_world.jpg')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download