我目前正在使用一个Python机器人来输出带有文本的图像,但是我发现文本常常太长而无法显示。因此,我决定把字符串分成两行,这样它就可以适合我的图像。我利用枕头5.1的图像处理。你知道吗
我是Python编程的新手,我尝试过搜索如何将Python字符串拆分为两行或更多行。不幸的是,所有的结果似乎只处理明确给定的字符串(即“string”)。你知道吗
print("Ababoubian wisdom!")
ababou2 = ababou() #returns a string, how to split into two lines?
if("Ababou" in ababou2):
ababou2 = ababou()
font = ImageFont.truetype("Arial.ttf", 14)
img = Image.new('RGB', (300, 200), color = (random.randint(0, 255),random.randint(0, 255), random.randint(0, 255)))
d = ImageDraw.Draw(img)
d.text((0, 0), ababou2, font=font) #draws text
预期结果:文本应环绕到下一行。你知道吗
我不确定一个文本包装器是否能帮助你,因为你需要分别画每一行。你知道吗
似乎应该计算单行中字符的最大长度,然后执行以下操作:
因为您要处理字体,所以您需要了解^{} 方法。这将帮助您了解如何拆分文本。你知道吗
假设您有一个字符串:
现在,您还有一个图像,其宽度和高度为:
如果文本太长,应缩短:
你怎么能缩短它?首先,尝试使用单词边界:
这会调用
getsize
很多次,这可能会很昂贵。(或不是:如果你是从猫图片中产生模因,可能不会花很多钱,因为文字很短。如果你在写文字处理器,当心!)你知道吗另一种方法是计算原始文本的大小,然后假设所有字符的宽度相同,并根据图像大小与文本大小的比率猜测拆分的位置:
注意,这是基于字符的,而不是基于单词的,所以有点糟糕。由于大多数字体是成比例的,这个比例可能是错误的。您可以使用一些启发式方法来调整它,包括一组内置的宽度假设。(只需计算一次所有字母的宽度,将它们存储在一个表中,并假设所有字体都使用该宽度-仍然是错误的,但通常比比率法更接近,而且更快!)你知道吗
相关问题 更多 >
编程相关推荐