Rendering Persian (Farsi) words in PIL for Python
Question: I am trying to make images based on Persian (Farsi) text. I am using PIL for
Python3. Here is my code:

    
    
    from PIL import Image, ImageFont, ImageDraw
    text = "خطاب"
    image = Image.new("RGBA", (100,100), (255,255,255))
    font = ImageFont.truetype("FreeFarsiMono.ttf", 60, encoding='unic')
    draw = ImageDraw.Draw(image)
    draw.text((0,0), text, (0,0,0), font=font)
    image.save("Test.png")
    image.show() 
    

However when I run the code I get some rectangular boxes with question mark in
them, instead of the image of the text! I would appreciate any help on this.


Answer: First you have to set the coding of the source:

    
    
    # -*- coding: utf-8 -*-
    

Then you have to decode your text:

    
    
    text = text.decode('utf-8')
    

Your final code should be sth like this:

    
    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    from PIL import Image, ImageFont, ImageDraw
    
    text = "ﻡﻼﺳ"
    text = text.decode('utf-8')
    image = Image.new("RGBA", (100,100), (255,255,255))
    font = ImageFont.truetype("yekan.ttf", 45, encoding='unic')
    draw = ImageDraw.Draw(image)
    draw.text((0,0), text, (0,0,0), font=font)
    image.save("Test.png")
    image.show()
    

_Note_ If you had your letters separated, you'd convert them; e.g. you can use
[awebfont](http://awebfont.ir/convert/) to solve that problem.

Output:

![Test.png](http://i.stack.imgur.com/XkI1T.png)

