簡體   English   中英

Python Pillow 多色文字

[英]Python Pillow Multi-color text

我無法將一串文本作為輸入並生成一行連續的文本,其中每兩個字符都有一種不同的(指定的)顏色。 我當前的代碼如下所示:

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)

我找到了我的問題的答案,因為網上沒有回復,所以我將我自己的答案發布給那些可能會偶然發現的人:

您計算前一個文本片段的長度,然后將其添加到起始 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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM