First, you need to have the PIL python module and import this.
1 2 3 4 | 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.
1 2 3 4 5 | 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.
1 2 3 | text = " Another script with python language! " font = ImageFont.truetype('Arggotsc.ttf', 55) image = Image.new("1", font.getsize(text), '#FFF') |
Now we can draw, add text, resize, but the text and finally save the image.
1 2 3 4 5 6 7 8 9 | 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 output image is this:
See you later with another tutorial.