I have a python script that is writing text to images using the PIL. Everything this is working fine except for when I encounter strings with carriage returns in them. I need to preserve the carriage returns in the text. Instead of writing the carriage return to the image, I get a little box character where the return should be. Here is the code that is writing the text:
我有一个python脚本,使用PIL将文本写入图像。这一切都很好,除非我遇到带有回车符的字符串。我需要在文本中保留回车符。而不是将回车写入图像,我得到一个小盒子字符,返回应该是。以下是编写文本的代码:
<code>
draw = ImageDraw.Draw(blankTemplate)
draw.text((35 + attSpacing, 570),str(attText),fill=0,font=attFont)
</code>
attText is the variable that I am having trouble with. I'm casting it to a string before writing it because in some cases it is a number.
attText是我遇到麻烦的变量。我在写之前将它转换为字符串,因为在某些情况下它是一个数字。
Thanks for you help.
谢谢你的帮助。
2 个解决方案
#1
Let's think for a moment. What does a "return" signify? It means go to the left some distance, and down some distance and resume displaying characters.
让我们想一下。 “回归”意味着什么?这意味着向左移动一段距离,向下移动一段距离并继续显示字符。
You've got to do something like the following.
你必须做类似以下的事情。
y, x = 35, 570
for line in attText.splitlines():
draw.text( (x,y), line, ... )
y = y + attSpacing
#2
You could try the the following code which works perfectly good for my needs:
您可以尝试以下代码,它完全符合我的需求:
# Place Text on background
lineCnt = 0
for line in str(attText):
draw = ImageDraw.Draw(blankTemplate)
draw.text((35 + attSpacing,570 + 80 * lineCnt), line, font=attFont)
lineCnt = lineCnt +1
The expression "y+80*lineCnt" moves the text down y position depending on the line no. (the factor "80" for the shift must be adapted according the font).
表达式“y + 80 * lineCnt”根据行号将文本向下移动y位置。 (换档因子“80”必须根据字体进行调整)。
#1
Let's think for a moment. What does a "return" signify? It means go to the left some distance, and down some distance and resume displaying characters.
让我们想一下。 “回归”意味着什么?这意味着向左移动一段距离,向下移动一段距离并继续显示字符。
You've got to do something like the following.
你必须做类似以下的事情。
y, x = 35, 570
for line in attText.splitlines():
draw.text( (x,y), line, ... )
y = y + attSpacing
#2
You could try the the following code which works perfectly good for my needs:
您可以尝试以下代码,它完全符合我的需求:
# Place Text on background
lineCnt = 0
for line in str(attText):
draw = ImageDraw.Draw(blankTemplate)
draw.text((35 + attSpacing,570 + 80 * lineCnt), line, font=attFont)
lineCnt = lineCnt +1
The expression "y+80*lineCnt" moves the text down y position depending on the line no. (the factor "80" for the shift must be adapted according the font).
表达式“y + 80 * lineCnt”根据行号将文本向下移动y位置。 (换档因子“80”必须根据字体进行调整)。