gpt4 book ai didi

python - PIL 使用转义序列在图像上写入文本

转载 作者:行者123 更新时间:2023-12-05 05:31:06 27 4
gpt4 key购买 nike

`from PIL import Image, ImageDraw, ImageFont

image = Image.new('RGB', (950, 250), color=(255, 255, 255))
TEXT = 'You are a wondeful \033[32mperson.'
font_size = 50
font_type = "SourceCodePro-Bold.ttf"
draw = ImageDraw.Draw(im=image)
font = ImageFont.truetype(font_type, font_size)
draw.multiline_text((int(950 / 2), int(250 / 2)), text=TEXT, font=font,fill=(0, 0, 0), anchor='mm')
image.show()`

我尝试使用 PIL(Pillow)使用一些转义序列“\033[32m”在图像上打印文本。

我期待下面的输出图像

我想要什么 expectation

我得到了什么 [enter image description here](/image/j2xdC.png)

所以任何人都知道如何获得所需的结果,这将非常有帮助。

谢谢。

最佳答案

你可以试试这个,改变文本的位置。我认为这个逻辑会对你有所帮助。谢谢

from PIL import Image, ImageDraw, ImageFont
image = Image.new('RGB', (950, 250), "white")
text1 = "You are a wondeful"
text2="Person"
font_size = 100
font_type = "SourceCodePro-Bold.ttf"
draw = ImageDraw.Draw(im=image)
font = ImageFont.load_default()
draw.multiline_text((int(950 / 2), int(250 / 2)), text=text1, font=font,fill ="black",anchor='mm')
draw.multiline_text((int(1180 / 2), int(250 / 2)), text=text2, font=font,fill = "green",anchor='mm')
image.show()

关于python - PIL 使用转义序列在图像上写入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74441103/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com