Guest User

Untitled

a guest
Apr 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. # Tested on Python 3.6.1
  2.  
  3. # install: pip install --upgrade arabic-reshaper
  4. import arabic_reshaper
  5.  
  6. # install: pip install python-bidi
  7. from bidi.algorithm import get_display
  8.  
  9. # install: pip install Pillow
  10. from PIL import ImageFont
  11. from PIL import Image
  12. from PIL import ImageDraw
  13.  
  14. # use a good font!
  15. fontFile = "/Users/amirreza/pil/Sahel.ttf"
  16.  
  17. # this was a 400x400 jpg file
  18. imageFile = "/Users/amirreza/pil/input.jpg"
  19.  
  20. # load the font and image
  21. font = ImageFont.truetype(fontFile, 18)
  22. image = Image.open(imageFile)
  23.  
  24. # firts you must prepare your text (you dont need this for english text)
  25. text = "سلام ایران"
  26. reshaped_text = arabic_reshaper.reshape(text) # correct its shape
  27. bidi_text = get_display(reshaped_text) # correct its direction
  28.  
  29. # start drawing on image
  30. draw = ImageDraw.Draw(image)
  31. draw.text((0, 0), bidi_text, (255,255,255), font=font)
  32. draw = ImageDraw.Draw(image)
  33.  
  34. # save it
  35. image.save("output.png")
Add Comment
Please, Sign In to add comment