首页 > 解决方案 > Python Pillow 多色文字

问题描述

我无法将一串文本作为输入并生成连续的文本行,其中每两个字符都有不同的(指定的)颜色。我当前的代码如下所示:

def gen_name(draw, stars, rank, name, color):
    # color = {
    #     'bracket_color': (231, 79, 80),
    #     'rank_color': (255, 255, 255),
    #     'plus_color': None
    # }
    font = 'fonts/Minecraftia-Regular.ttf'
    size = 100
    _text(draw, 20, 20, f'[{stars}✫]', size, (180, 180, 180), background=False, font=font)
    if rank != 'MEMBER':
        _text(draw, 60, 20, '[', size, color['bracket_color'], background=False, font=font)
        _text(draw, 70, 20, rank, size, color['rank_color'], background=False, font=font)
        if '+' in rank:
            _text(draw, 80, 20, rank[rank.index('+'):], size, color['plus_color'], background=False, font=font)
            _text(draw, 85, 20, ']', size, color['bracket_color'], background=False, font=font)
            _text(draw, 95, 20, name, size, color['bracket_color'], background=False, font=font)
        else:
            _text(draw, 80, 20, ']', size, color['bracket_color'], background=False, font=font)
            _text(draw, 90, 20, name, size, color['bracket_color'], background=False, font=font)
    else:
        _text(draw, 30, 20, name, size, color['rank_color'], background=False, font=font)

标签: pythonpython-3.xpython-imaging-library

解决方案


我找到了我的问题的答案,因为网上没有回复,所以我将我自己的答案发布给那些可能偶然发现的人:

您计算前一个文本片段的长度,然后将其添加到起始 x 值,然后直到您没有文本继续这样做。

def gen_name(draw, stars, rank, name, color):
    # stats.rank_color = {
    #     'bracket_color': (231, 79, 80),
    #     'rank_color': (255, 255, 255),
    #     'plus_color': None
    # }
    font = 'fonts/Minecraftia-Regular.ttf'
    size = 50
    f = ImageFont.truetype(font=font, size=size)
    t = f'[{stars}]'
    x = 20
    y = 20
    _text(draw, x, y, t, size, (180, 180, 180), background=False, font=font)
    x = x + draw.textsize(t, font=f)[0]
    if rank != 'MEMBER':
        t = '['
        _text(draw, x, y, t, size, color['bracket_color'], background=False, font=font)
        x = x + draw.textsize(t, font=f)[0]
        t = rank[:rank.index('+')]
        _text(draw, x, y, t, size, color['rank_color'], background=False, font=font)
        x = x + draw.textsize(t, font=f)[0]
        if '+' in rank:
            t = rank[rank.index('+'):]
            _text(draw, x, y, t, size, color['plus_color'], background=False, font=font)
            x = x + draw.textsize(t, font=f)[0]
            t = ']'
            _text(draw, x, y, t, size, color['bracket_color'], background=False, font=font)
            x = x + draw.textsize(t, font=f)[0]
            t = name
            _text(draw, x, y, t, size, color['bracket_color'], background=False, font=font)
            x = x + draw.textsize(t, font=f)[0]
        else:
            t = ']'
            _text(draw, x, y, t, size, color['bracket_color'], background=False, font=font)
            x = x + draw.textsize(t, font=f)[0]
            t = name
            _text(draw, x, y, t, size, color['bracket_color'], background=False, font=font)
            x = x + draw.textsize(t, font=f)[0]
    else:
        t = name
        _text(draw, x, y, t, size, color['rank_color'], background=False, font=font)
        x = x + draw.textsize(t, font=f)[0]

推荐阅读