本文介绍了使用Python的文字阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用PIL在图片上添加了一些文字

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

但是,这种方法不允许阴影半径,不透明的css样式属性.

However, such an approach does not allow for shadow-radius, opacity css-style properties.

是否有更好的方法来使用Python创建文本阴影?如果可以,怎么办?

Is there a better way to create a text shadow with Python? If so, how?

推荐答案

看看这些示例.

示例1

示例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)

这篇关于使用Python的文字阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 12:05