gpt4 book ai didi

python - 一旦超过一定的宽度,就沿着空格打破一个字符串(Python)

转载 作者:行者123 更新时间:2023-11-28 18:22:53 28 4
gpt4 key购买 nike

我正在基础图像上渲染文本。

一个核心要求是,只要字符的总宽度超过基本图像的宽度,字符串就换行到下一行。我通过以下正确工作的代码段完成此操作:

base_width, base_height = base_img.size
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", font_size)
line_width = 0
line_count = 1
lines = []
string = ""
for c in text:
line_width += font.getsize(c)[0]
string+=str(c)
if line_width > base_width:
lines.append(string)
string = ""
line_width = 0
line_count += 1
if string:
lines.append(string)

结果 lines 是通过分解原始字符串获得的子字符串列表。

现在我需要改进这个算法。

问题是它打断了 mid-word 行。例如。字符串 lorem ipsum 可能以 lines = ['lorem ip','sum'] 结束。相反,对我来说,理想的分手是更具人类可读性的 lines = ['lorem','ipsum']lines = ['lorem','ipsum']

换句话说,我想沿着空格而不是中间的单词断开线条。有人可以给我一个说明性的例子来说明我如何做到这一点吗?我似乎无法理解它。

最佳答案

这里尝试让您的代码以最少的更改和大量的调试输出工作:

#!python3
#coding=utf-8
""" Line break demo 2 """

text = "lorem ipsum dolor sit amet blablah"

for wmax in [10,25,55,80,100,120]:

print(wmax)

base_width, base_height = (wmax,None)#base_img.size
#font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", font_size)
line_width = 0
line_count = 1
lines = []
string = ""
for c in text:
line_width += 5#font.getsize(c)[0]
string += c
if line_width > base_width:

print("text ", text)
print("string", string)

s = string.rsplit(" ", 1)
print("split ", s)

string = s[0]
lines.append(string)

try:
string = s[1]
line_width = len(string) * 5
except:
string = ""
line_width = 0

print("lines ", lines)
print("string", string)

line_count += 1
print()

if string:
lines.append(string)


print(lines)
print()

输出:

10
text lorem ipsum dolor sit amet blablah
string lor
split ['lor']
lines ['lor']
string

text lorem ipsum dolor sit amet blablah
string em
split ['em', '']
lines ['lor', 'em']
string

text lorem ipsum dolor sit amet blablah
string ips
split ['ips']
lines ['lor', 'em', 'ips']
string

text lorem ipsum dolor sit amet blablah
string um
split ['um', '']
lines ['lor', 'em', 'ips', 'um']
string

text lorem ipsum dolor sit amet blablah
string dol
split ['dol']
lines ['lor', 'em', 'ips', 'um', 'dol']
string

text lorem ipsum dolor sit amet blablah
string or
split ['or', '']
lines ['lor', 'em', 'ips', 'um', 'dol', 'or']
string

text lorem ipsum dolor sit amet blablah
string sit
split ['sit']
lines ['lor', 'em', 'ips', 'um', 'dol', 'or', 'sit']
string

text lorem ipsum dolor sit amet blablah
string am
split ['', 'am']
lines ['lor', 'em', 'ips', 'um', 'dol', 'or', 'sit', '']
string am

text lorem ipsum dolor sit amet blablah
string ame
split ['ame']
lines ['lor', 'em', 'ips', 'um', 'dol', 'or', 'sit', '', 'ame']
string

text lorem ipsum dolor sit amet blablah
string t b
split ['t', 'b']
lines ['lor', 'em', 'ips', 'um', 'dol', 'or', 'sit', '', 'ame', 't']
string b

text lorem ipsum dolor sit amet blablah
string bla
split ['bla']
lines ['lor', 'em', 'ips', 'um', 'dol', 'or', 'sit', '', 'ame', 't', 'bla']
string

text lorem ipsum dolor sit amet blablah
string bla
split ['bla']
lines ['lor', 'em', 'ips', 'um', 'dol', 'or', 'sit', '', 'ame', 't', 'bla', 'bla
string

['lor', 'em', 'ips', 'um', 'dol', 'or', 'sit', '', 'ame', 't', 'bla', 'bla', 'h']

25
text lorem ipsum dolor sit amet blablah
string lorem
split ['lorem', '']
lines ['lorem']
string

text lorem ipsum dolor sit amet blablah
string ipsum
split ['ipsum', '']
lines ['lorem', 'ipsum']
string

text lorem ipsum dolor sit amet blablah
string dolor
split ['dolor', '']
lines ['lorem', 'ipsum', 'dolor']
string

text lorem ipsum dolor sit amet blablah
string sit am
split ['sit', 'am']
lines ['lorem', 'ipsum', 'dolor', 'sit']
string am

text lorem ipsum dolor sit amet blablah
string amet b
split ['amet', 'b']
lines ['lorem', 'ipsum', 'dolor', 'sit', 'amet']
string b

text lorem ipsum dolor sit amet blablah
string blabla
split ['blabla']
lines ['lorem', 'ipsum', 'dolor', 'sit', 'amet', 'blabla']
string

['lorem', 'ipsum', 'dolor', 'sit', 'amet', 'blabla', 'h']

55
text lorem ipsum dolor sit amet blablah
string lorem ipsum
split ['lorem ipsum', '']
lines ['lorem ipsum']
string

text lorem ipsum dolor sit amet blablah
string dolor sit am
split ['dolor sit', 'am']
lines ['lorem ipsum', 'dolor sit']
string am

text lorem ipsum dolor sit amet blablah
string amet blablah
split ['amet', 'blablah']
lines ['lorem ipsum', 'dolor sit', 'amet']
string blablah

['lorem ipsum', 'dolor sit', 'amet', 'blablah']

80
text lorem ipsum dolor sit amet blablah
string lorem ipsum dolor
split ['lorem ipsum', 'dolor']
lines ['lorem ipsum']
string dolor

text lorem ipsum dolor sit amet blablah
string dolor sit amet bl
split ['dolor sit amet', 'bl']
lines ['lorem ipsum', 'dolor sit amet']
string bl

['lorem ipsum', 'dolor sit amet', 'blablah']

100
text lorem ipsum dolor sit amet blablah
string lorem ipsum dolor sit
split ['lorem ipsum dolor', 'sit']
lines ['lorem ipsum dolor']
string sit

['lorem ipsum dolor', 'sit amet blablah']

120
text lorem ipsum dolor sit amet blablah
string lorem ipsum dolor sit ame
split ['lorem ipsum dolor sit', 'ame']
lines ['lorem ipsum dolor sit']
string ame

['lorem ipsum dolor sit', 'amet blablah']

关于python - 一旦超过一定的宽度,就沿着空格打破一个字符串(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43828154/

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