python - 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)
解决方案
我找到了我的问题的答案,因为网上没有回复,所以我将我自己的答案发布给那些可能偶然发现的人:
您计算前一个文本片段的长度,然后将其添加到起始 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]
推荐阅读
- python - Blender Python:取消运算符而不关闭 props_dialog
- python - 如何计算python字典中列表元素的平均值?
- typescript - 无法使用 yarn tsc 编译 TypeScript 项目
- javascript - 单元格如何监听 mxGraph 中的移动或调整大小事件?
- c++ - 具有多种排序和集合的映射(例如不是 [] 类型的映射索引)
- python - 基于打包(setup.py)从另一个文件导入函数导致“未找到.egg-info目录”
- ubuntu - 有没有办法测试 VS Code 是否在 Ubuntu 上解析我的 shell 环境?
- c - C中字符的条件
- php - 如何在 php 中翻阅 Google Cloud Storage 的结果?
- pandas-groupby - 我刚刚做了一个 groupby 函数,但我试图只显示成功和失败持续时间的平均值