使用枕头绘制草书文字 [英] Using Pillow to draw cursive text
问题描述
我要在Ubuntu 14.04 LTS
机器上托管的Django应用程序中的图像上绘制文本. Pillow 4.2.1
是我的选择库.
I'm to draw text over an image in a Django application hosted on an Ubuntu 14.04 LTS
machine. Pillow 4.2.1
is my lib of choice.
我已经通过从PIL
导入的ImageDraw
成功完成了此任务(实际代码在此问题的结尾)
I've successfully accomplished this task via ImageDraw
imported from PIL
(The actual code is at the end of this question)
我的代码非常适合英语,法语或西班牙语等语言.
My code works perfectly for languages such as English, French or Spanish.
但不适用于自然草书语言,例如阿拉伯语,波斯语或乌尔都语.在这种情况下,它将分别绘制每个字母.例如. فارسی
(波斯语)绘制为:
But not for naturally cursive languages like Arabic, Persian or Urdu. In such cases, it draws each letter separately. E.g. فارسی
(Persian) is drawn as:
请注意,我已安装sudo apt-get install ttf-mscorefonts-installer
并为此尝试了/fonts/truetype/msttcorefonts/Arial.ttf
.
Note that I installed sudo apt-get install ttf-mscorefonts-installer
and tried /fonts/truetype/msttcorefonts/Arial.ttf
for this.
有人向我提出建议,以确保我使用的字体具有连字.我的理解是Arial
确实支持连字,但问题仍然存在.
Someone advised me to make sure the font I'm using has ligatures. My understanding is that Arial
does support ligatures, yet the problem persists.
我的问题是:我应该怎么做才能解决此问题?我的代码必须支持自然草书语言,例如阿拉伯语,波斯语或乌尔都语.
My question is: what should I do to fix this issue? My code must support naturally cursive languages like Arabic, Persian or Urdu.
代码:
from PIL import ImageDraw
draw = ImageDraw.Draw(img)
base_width, base_height = img.size
y = 2
for line in lines:
width, height = font.getsize(line)
x = (base_width - width) / 2
text_with_stroke(draw,x,y,line,font,fillcolor,shadowcolor)
y += height
其中text_with_stroke
很简单:
def text_with_stroke(draw,width,height,line,font,fillcolor,shadowcolor):
draw.text((width-1, height), line, font=font, fill=shadowcolor)
draw.text((width+1, height), line, font=font, fill=shadowcolor)
draw.text((width, height-1), line, font=font, fill=shadowcolor)
draw.text((width, height+1), line, font=font, fill=shadowcolor)
draw.text((width, height), line, font=font, fill=fillcolor)
简而言之,此代码考虑字体大小和基础图像大小,将任何给定的文本分成几行.然后,它会在图像上反复绘制每一行文本.
In a nutshell, this code breaks any given text into separate lines, taking into account the font size and underlying image size. It then iteratively draws each line of text on the image.
推荐答案
The best answer for this problem is already tackled on this rather excellent SO post: https://stackoverflow.com/a/25727238/4936905.
本质上,这里需要两个Python库:BiDi
和Arabic Reshaper
.
Essentially, two Python libraries are needed here: BiDi
and Arabic Reshaper
.
这是pip install python-bidi
和pip install git+https://github.com/mpcabd/python-arabic-reshaper
.确切的实现需要在通过PIL
绘制text
之前按以下方式进行转换:
That's pip install python-bidi
and pip install git+https://github.com/mpcabd/python-arabic-reshaper
. The exact implementation entails transforming text
as follows before drawing it via PIL
:
reshaped_text = arabic_reshaper.reshape(line)
final_text = get_display(reshaped_text)
draw.text((width-1, height), final_text, font=font, fill=shadowcolor)
draw.text((width+1, height), final_text, font=font, fill=shadowcolor)
draw.text((width, height-1), final_text, font=font, fill=shadowcolor)
draw.text((width, height+1), final_text, font=font, fill=shadowcolor)
draw.text((width, height), final_text, font=font, fill=fillcolor)
这篇关于使用枕头绘制草书文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!