linux CLI:如何将阿拉伯文本呈现为位图

时间:2014-05-08 08:18:55

标签: ruby linux imagemagick arabic rmagick

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

我尝试过imagemagick和RMagick,但是我遇到了RTL语言问题。我所有的渲染都是从左到右。谷歌没有帮助。

需要:

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

会给出一个位图:

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

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

2 个答案:

答案 0 :(得分:8)

  

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

我可以。我使用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

结果如下:

Text to image result

一些注意事项:

  1. 您需要使用定义已使用字符的字体(在您的情况下为阿拉伯字体)
  2. 始终使用内部文本的临时文件,而不是直接从命令行提供文本:这会导致奇怪的结果
  3. 更新我没有注意到问题的RTL部分;我认为使用pango(reference)得到了更好的结果:

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

    结果:

    Text to image result using pango

答案 1 :(得分:1)

对于阿拉伯语排版,您可能需要使用 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>

enter image description here

您可以用系统中的任何字体替换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)

Arabic typography in images