Question

I would like to draw Arabic text into bitmap (bmp or png) with command line interface.

I have tried imagemagick and RMagick, but I have problem with RTL language issue. all my rendering are left to right. And google is not helping.

require:

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

would give a bitmap:

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

Anyone can give me some successful results?

Was it helpful?

Solution

Anyone can give me some successful results?

I can. I use convert from ImageMagick:

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

This is the result:

Text to image result

Some notes:

  1. You need to use a font which defines the used characters (in your case an arabic font)
  2. Always use a temporary file with the text inside instead of provide the text directly from the command line: it would lead to weird results

UPDATE I didn't notice the RTL part of the question; I think I got better results using pango (reference):

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

Result:

Text to image result using pango

OTHER TIPS

For Arabic typography, you might want to use Inkscape 0.9 as follows (Window7 cmd):

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

Where ar.svg is:

<?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

You can replace the font-family:'Droid Arabic Naskh' by any font in your system. Also you can change the output format as stated in the man pages of Inkscape. Moreover, you can start in Inkscape GUI and save the SVG and change what you want in the SVG XML in any scripting language.

Inkscape 0.48 returned buggy Arabic typography.

As for ImageMagick 6.8.9-7, I used Python +Wand(Python Lib)+arabic_reshaper(Python Lib)+bidi.algorithme(Python Lib) to generate correct Arabic typography in images:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top