答案 0 :(得分:0)
我设法生成了以下图像:
使用该脚本:
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, )