Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make text shadow effect in pillow python?

So far I have achieved this with the pillow, but it's not much visible on some photos, so I want to add some shadow behind the image(called lift effect in texts) like this which is basically the addition of two texts, one with a blur version and the second is normal text on top with a little smaller size (please check the second image to understand) I can't find anywhere about how to blur the text, so would be really nice if someone can help me out

like image 886
AbHi Avatar asked Oct 27 '25 05:10

AbHi


1 Answers

Not 100% certain what you have to start with or what you really want to end up with, but here is a technique that can give you a soft shadow behind text. Basically, you do the following:

  • create a spare piece of canvas big enough for your text
  • write your text on the canvas
  • blur it to soften it
  • paste that onto the original image
  • write the sharp text on top

There are a million variations, with different type of blur, different font sizes, different under-colours (shadow colours) and different ways of pasting/compositing the blurred text onto the original. But if you take the code below as a starting point you can experiment till you are happy.

#!/usr/bin/env python3
# See also: https://legacy.imagemagick.org/Usage/fonts/#soft_shadow
from PIL import Image, ImageDraw, ImageFont, ImageFilter, ImageChops

# Open background image and work out centre
bg = Image.open('bg.png').convert('RGB')
x = bg.width//2
y = bg.height//2

# The text we want to add
text = "StackOverflow"

# Create font
font = ImageFont.truetype('/System/Library/Fonts/MarkerFelt.ttc', 60)

# Create piece of canvas to draw text on and blur
blurred = Image.new('RGBA', bg.size)
draw = ImageDraw.Draw(blurred)
draw.text(xy=(x,y), text=text, fill='cyan', font=font, anchor='mm')
blurred = blurred.filter(ImageFilter.BoxBlur(7))

# Paste soft text onto background
bg.paste(blurred,blurred)

# Draw on sharp text
draw = ImageDraw.Draw(bg)
draw.text(xy=(x,y), text=text, fill='navy', font=font, anchor='mm')
bg.save('result.png')

Original image

enter image description here

Result image

enter image description here

like image 55
Mark Setchell Avatar answered Oct 28 '25 17:10

Mark Setchell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!