使用PIL使用回车写入文本返回到Python中的图像 [英] Writing text with carriage return to image in Python using PIL

查看:102
本文介绍了使用PIL使用回车写入文本返回到Python中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用PIL将文本写入图像的python脚本。这一切都很好,除非我遇到带有回车符的字符串。我需要在文本中保留回车符。而不是将回车写入图像,我得到一个小盒子字符,返回应该是。以下是编写文本的代码:

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:

<code>
 draw = ImageDraw.Draw(blankTemplate)
 draw.text((35 + attSpacing, 570),str(attText),fill=0,font=attFont)
</code>

attText是我遇到问题的变量。我在写之前将它转换为字符串,因为在某些情况下它是一个数字。

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.

谢谢你的帮助。

推荐答案

让我们想一想。 回归意味着什么?这意味着向左移动一段距离,向下移动一段距离并继续显示字符。

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.

您必须执行以下操作。

y, x = 35, 570
for line in attText.splitlines():
    draw.text( (x,y), line, ... )
    y = y + attSpacing

这篇关于使用PIL使用回车写入文本返回到Python中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆