I just wanted to write some text on picture (possibly with some simple effects like shadow). How can I do this with PIL?
Asked
Active
Viewed 9,308 times
5
-
Are there non-PIL alternatives? – Mircea Jun 21 '19 at 09:20
1 Answers
5
First install Python Imaging Library (pip install Pillow
)
Note: you may need to change the path to your font file font_fname
.
import numpy as np
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 = np.dot(np.ones((h,w,3), dtype='uint8'), np.diag(np.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)')
image0.save('hello_world.jpg')

wim
- 338,267
- 99
- 616
- 750
-
How to add the special effects (like shadows, dilation, noise, etc)? – NightFury13 Sep 15 '17 at 13:08