I have added some text over an image using PIL
I'd like to add a text shadow, with a certain shadow radius and shadow opacity.
I've been able to fake this a bit (it doesn't work too well) by drawing a shadow, before I draw some text, and place it a little bit above the text.
draw.text((x, y + 2), text, font = some_font, fill = (208,208,208)) #shadow
draw.text((x, y), text, font = some_font, fill = (255,255,255)) #text
However, such an approach does not allow for shadow-radius, opacity css-style properties.
Is there a better way to create a text shadow with Python? If so, how?
Have a look at these examples.
Example 1
Example 2
and the last one is kinda similar to what you attempted.
import Image, ImageFont, ImageDraw
import win32api, os
x, y = 10, 10
fname1 = "c:/test.jpg"
im = Image.open(fname1)
pointsize = 30
fillcolor = "red"
shadowcolor = "yellow"
text = "hi there"
font = win32api.GetWindowsDirectory() + "\\Fonts\\ARIALBD.TTF"
draw = ImageDraw.Draw(im)
font = ImageFont.truetype(font, pointsize)
# thin border
draw.text((x-1, y), text, font=font, fill=shadowcolor)
draw.text((x+1, y), text, font=font, fill=shadowcolor)
draw.text((x, y-1), text, font=font, fill=shadowcolor)
draw.text((x, y+1), text, font=font, fill=shadowcolor)
# thicker border
draw.text((x-1, y-1), text, font=font, fill=shadowcolor)
draw.text((x+1, y-1), text, font=font, fill=shadowcolor)
draw.text((x-1, y+1), text, font=font, fill=shadowcolor)
draw.text((x+1, y+1), text, font=font, fill=shadowcolor)
# now draw the text over it
draw.text((x, y), text, font=font, fill=fillcolor)
fname2 = "c:/test2.jpg"
im.save(fname2)
os.startfile(fname2)
Your best bet is something along the lines of:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With