linux CLI:如何将阿拉伯文字渲染成位图

我想用命令行界面将阿拉伯语文本绘制成位图(bmp或png)。

我曾尝试imagemagick和RMagick,但我有RTL语言问题的问题。 我所有的渲染都保持不变。 而谷歌没有帮助。

要求:

./render "لوحة المفاتيح" out.png 

会给一个位图:

لوحةالمفاتيح

任何人都可以给我一些成功的结果?

任何人都可以给我一些成功的结果?

我可以。 我使用从ImageMagick convert

 echo لوحة المفاتيح > text.txt && \ convert -size 300x200 \ -background white \ -fill black \ -font /usr/share/fonts/truetype/droid/DroidNaskh-Regular.ttf \ -pointsize 24 \ -gravity Center \ label:@text.txt \ text.png 

这是结果:

文本到图像的结果

一些说明:

  1. 你需要使用一个字体来定义使用的字符(在你的情况下是一个阿拉伯字体)
  2. 总是在文本中使用临时文件,而不是直接从命令行提供文本:这会导致不正常的结果

更新我没有注意到问题的RTL部分; 我想我使用pango( 参考 )得到了更好的结果:

 # On Ubuntu sudo apt-get install libpango1.0-dev echo -n لوحة المفاتيح > text.txt pango-view text.txt --no-display --output text.png 

结果:

文本到图像结果使用pango

对于阿拉伯文版式,您可能需要使用Inkscape 0.9 ,如下所示(Window7 cmd):

 inkscape "ar.svg" --export-png="C:\Users\Path\TO\output\arabicthabit.png" --export-dpi="900,900" --export-background="rgb(100%,100%,100%)" 

ar.svg是:

 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" width="400" height="100" version="1.1"> <text xml:space="preserve" style="font-size:30px;font-style:normal;fill:#000000;fill-opacity:1;stroke:none;font-family:'Droid Arabic Naskh';font-weight:bold;" x="10" y="90" ><tspan>لغةٌ عربيّةٌ</tspan> </text> </svg> 

在这里输入图像说明

您可以用系统中的任何字体替换font-family:'Droid Arabic Naskh' 。 您也可以按照Inkscape手册中的说明更改输出格式。 此外,您可以从Inkscape GUI开始并保存SVG,并以任何脚本语言在SVG XML中更改所需内容。

Inkscape 0.48返回了有问题的阿拉伯语版式。

对于ImageMagick 6.8.9-7 ,我使用Python + Wand(Python Lib)+ arabic_reshaper(Python Lib)+ bidi.algorithme(Python Lib)在图像中生成正确的阿拉伯语版式:

 from wand.image import Image as wImage from wand.display import display as wdiplay from wand.drawing import Drawing from wand.color import Color import arabic_reshaper from bidi.algorithm import get_display reshaped_text = arabic_reshaper.reshape(u'لغةٌ عربيّة') artext = get_display(reshaped_text) fonts = ['C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\DroidNaskh-Bold.ttf', 'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\Thabit.ttf', 'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\Thabit-Bold-Oblique.ttf', 'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\Thabit-Bold.ttf', 'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\Thabit-Oblique.ttf', 'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\majalla.ttf', 'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\majallab.ttf', ] draw = Drawing() img = wImage(width=1200,height=(len(fonts)+2)*60,background=Color('#ffffff')) #draw.fill_color(Color('#000000')) draw.text_alignment = 'right'; draw.text_antialias = True draw.text_encoding = 'utf-8' #draw.text_interline_spacing = 1 #draw.text_interword_spacing = 15.0 draw.text_kerning = 0.0 for i in range(len(fonts)): font = fonts[i] draw.font = font draw.font_size = 40 draw.text(img.width / 2, 40+(i*60),artext) print draw.get_font_metrics(img,artext) draw(img) draw.text(img.width / 2, 40+((i+1)*60),u'ناصر test') draw(img) img.save(filename='C:\\PATH\\OUTPUT\\arabictest.png'.format(r)) wdiplay(img) 

在图像中的阿拉伯文版式