Linux命令行界面:如何将阿拉伯文本呈现为位图

3
我希望通过命令行界面将阿拉伯文本绘制到位图(bmp或png)中。我尝试过ImageMagick和RMagick,但在RTL语言方面存在问题,所有我的渲染都是从左到右的。谷歌也没有给予帮助。
要求:
./render "لوحة المفاتيح" out.png

会给出一个位图:

有人可以给我一些成功的结果吗?

键盘

2个回答

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

这是结果:

文本转图像结果

一些注意事项:

  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生成文本图像的结果


1
谢谢,但这是不正确的。阿拉伯语是从右到左书写的,正确的应该是:"لوحة المفاتيح",而你的是"حيتافملا ةحول"。 - c2h2
很抱歉,我没有注意到你问题中的RTL部分 :-/ - mdesantis
没关系,我不会说阿拉伯语,也不认识任何字符。确实很令人困惑。 - c2h2
我注意到设置dpi(例如使用“--dpi = 500”)会影响字体大小,但我没有进一步调查 :) - mdesantis

4

如果您需要进行阿拉伯文排版,可以按照以下步骤使用Inkscape 0.9(在Windows7命令提示符中):

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接