有没有办法用枕头绘制带有渐变颜色的文本?

时间:2018-12-28 00:13:33

标签: python python-imaging-library

我要创建一个带有文字的图像。到目前为止,一切正常。现在,为了进行微调,我认为使用渐变颜色的文本会很好。

这是我当前所要解决的问题。

current

这是我想要拥有的东西。

want

2 个答案:

答案 0 :(得分:0)

我设法生成了以下图像:

enter image description here

使用该脚本:

from PIL import Image, ImageFont, ImageDraw

OUTPUT_IMAGE = '53952270.png'
BG_COLOR = (0, 102, 0)
TEXT = 'STIEFELSTANGE'
TEXT_COLOR = (255, 255, 255)
SHADOW_COLOR = (231, 255, 227)

image = Image.new('RGB', (212, 45), color=BG_COLOR)
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('impact', 36)
text_size = font.getsize(TEXT)
draw.text((0, 0), TEXT, font=font)
pixels = image.load()
size = image.size

x_list = []
y_list = []
for x in range(size[0]):
    for y in range(size[1]):
        if pixels[x, y] == TEXT_COLOR:
            x_list.append(x)
            y_list.append(y)

shadow_height = text_size[1]/4
for x, y in zip(x_list, y_list):
    if y < min(y_list) + shadow_height or y > max(y_list)-shadow_height:
        pixels[x, y] = SHADOW_COLOR

image.save(OUTPUT_IMAGE)

答案 1 :(得分:0)

我已经做到了。不幸的是,对于巨大的Textsize(例如100),它真的很慢。

有人想要简化此功能吗?

它正在创建此文本,例如:example

    async def createTextWithOutline(self, image, x, y, text, font, outlineAmount, textColor, shadowColor, rarity):
    # create outline text
    drawObject = ImageDraw.Draw(image)
    for adjX in range(outlineAmount):
        for adjY in range(outlineAmount):
            drawObject.text((x + adjX, y + adjY), text, font=font, fill=shadowColor)
            drawObject.text((x + adjX, y - adjY), text, font=font, fill=shadowColor)
            drawObject.text((x - adjX, y + adjY), text, font=font, fill=shadowColor)
            drawObject.text((x - adjX, y - adjY), text, font=font, fill=shadowColor)
    drawObject.text((x, y), text, font=font, fill=textColor)
    if rarity != None:
        if 'legendary' == rarity:
            color = legendaryShadowColor
        elif 'epic' == rarity:
            color = epicShadowColor
        elif 'rare' == rarity:
            color = rareShadowColor
        elif 'uncommon' == rarity:
            color = uncommonShadowColor
        else:
            color = commonInnerColor
        x_list = []
        y_list = []
        pixels = image.load()
        size = drawObject.textsize(text, font=font)
        for i in range(int(x), int(x+size[0])):
            for j in range(int(y), int(y+size[1])):
                if pixels[i, j] == textColor + (255, ):
                    x_list.append(i)
                    y_list.append(j)
        shaderHeight = size[1]//3.5
        for i, j in zip(x_list, y_list):
            if j < min(y_list) + shaderHeight or j > max(y_list) - shaderHeight:
                pixels[i,j] = color + (255, )