First you need to have the PIL python module and import this.
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from PIL import ImageFilter
Next will be this function.Basically will make and return one RGBA image data type.
def make_img(image, textColor, backgroundColor):
img = image.convert("RGBA")
img.putdata([textColor if value == 0 else backgroundColor
for value in image.getdata()])
return img
The next step is to set the text , font and the new image.
I use Arggotsc.ttf. You can use any TrueType font.
text = " Inca un script in python! "
font = ImageFont.truetype('Arggotsc.ttf', 55)
image = Image.new("1", font.getsize(text), '#FFF')
Now we can draw, add text, resize, bur the text and finally save the image.
draw = ImageDraw.Draw(image)
draw.text((0, 0), text, font=font)
image = image.resize([i for i in image.size], Image.NEAREST)
imgText = make_img(image, (200, 200, 200), (0, 0, 0, 0))
blur_img = make_img(image, (0, 0, 0), (0, 0, 0, 0))
for i in range(3):
blur_img = blur_img.filter(ImageFilter.BLUR)
blur_img.paste(imgText, (0, 0), imgText)
blur_img.save("text-img.png")
The result is:
See you later with another tutorial.