有没有办法用 Pillow 绘制带有渐变颜色的文本? [英] Is there a way, to draw Text with a gradient color with Pillow?
本文介绍了有没有办法用 Pillow 绘制带有渐变颜色的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将要创建一个带有文字的图像.到目前为止,一切正常.现在,对于微调,我认为文本具有渐变颜色会很好.
I am about to create an image with text on it. So far everything is working fine. Now, for the fine-tuning, I thought it would be nice to have the text with a gradient color.
这是我目前的重点.
这就是我想要拥有的.
推荐答案
我设法生成了以下图像:
I've managed to generate following image:
使用该脚本:
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)
这篇关于有没有办法用 Pillow 绘制带有渐变颜色的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文