python-docx表格设置阴影_Python的文字阴影

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.

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值