How to make a stroke(text) pillow python?

ATTENTION! Below is a terrible code!

from PIL import Image
from PIL import ImageDraw
from PIL import ImageOps
from PIL import ImageFont
import time as t

k = 0
rsize = (600,600)
text = 'Аниме сила,\n брат мой  \n не так ли?'
f = ImageFont.truetype(("font.ttf"), 85)
test_image = Image.open('asd.png')
tex = Image.new("RGBA", rsize, color=(255, 255, 255, 0))
draw = ImageDraw.Draw(tex)
sidth = draw.textsize(" ", font=f)[0]
for i in text.splitlines():
    w, h = f.getsize(i)
    draw.text((((600-w)/2),345+((((h/2)+20)*k))), i, font=f, fill=(86,7,10))
    k += 1
test_image.alpha_composite(tex, (0, 0))
test_image.show()

The result of executing the code(which is higher) enter a description of the image here What you need to getenter a description of the image here

How do I make the same text?

Author: GREAT SKRAMBY, 2018-12-06

1 answers

If you can't find a ready-made font with an outline, then there is an option through "crutches" (as per https://stackoverflow.com/questions/41556771/is-there-a-way-to-outline-text-with-a-dark-line-in-pil?rq=1):

from PIL import Image, ImageDraw, ImageFont
img = Image.new('RGB',(140,30),'#fff')
d = ImageDraw.Draw(img)
text='StackOverflow'
font= ImageFont.truetype("arial.ttf", 20)

offset = 3
shadowColor = 'black'

imgWidth,imgHeight = img.size

x = 5
y = 2

for off in range(offset):
    #move right
    d.text((x-off, y), text, font=font, fill=shadowColor)
    #move left
    d.text((x+off, y), text, font=font, fill=shadowColor)
    #move up
    d.text((x, y+off), text, font=font, fill=shadowColor)
    #move down
    d.text((x, y-off), text, font=font, fill=shadowColor)
    #diagnal left up
    d.text((x-off, y+off), text, font=font, fill=shadowColor)
    #diagnal right up
    d.text((x+off, y+off), text, font=font, fill=shadowColor)
    #diagnal left down
    d.text((x-off, y-off), text, font=font, fill=shadowColor)
    #diagnal right down
    d.text((x+off, y-off), text, font=font, fill=shadowColor)


d.text((x,y), text, font=font, fill="#fff")
del d
img.save('soimg.png')

enter a description of the image here

 1
Author: strawdog, 2018-12-06 09:36:17