3
from PIL import Image, ImageFont, ImageDraw

from matplotlib import pyplot as plt
import numpy as np
import cv2

text_string = u'تصوير'

img = Image.new('RGB', (200, 150))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('C:\\Windows\\Nafees Nastaleeq Urdu Font\\Nafees Nastaleeq(Updated).ttf', 50)

draw.text((25,40), text_string, fill='white', font=font)

img.show()

I want to show text on image but only urdu alphabets shown on image.

I want urdu ligature shown on image but in output only urdu alphabets shown on image.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
fatima
  • 31
  • 2
  • The same problem occurs with Arabic: https://stackoverflow.com/questions/39000761 – Karl Knechtel Feb 28 '23 at 07:25
  • Setting the layout engine `ImageFont.truetype(..., layout_engine=ImageFont.Layout.RAQM)` is required for some other Asian scripts, but I don't know if it applies to Urdu script. This would require RAQM, but [installing RAQM for Windows can be difficult](https://stackoverflow.com/q/57545244/5675094), so you might have to test it out on Linux / Mac environments to see if it helps... – mimocha Feb 28 '23 at 08:46
  • how set engine i dont understand – fatima Feb 28 '23 at 09:26
  • @fatima matplotlib, PIllow and others can support a number of rendering engines for text, Pillow can support libraqm (Raqm) which uses either FriBiDi or Sheen for bidirectional algorithm, and Harfbuzz for OpenType font rendering. Soem versions of Pillow/PIL will use Raqm by default if installed. See https://github.com/HOST-Oman/libraqm. You first need to install Harfbuzz and FriBiDI (or Sheen), then install Raqm. You may need to reinstall PIL. Examples of using Raqm this way: https://github.com/enabling-languages/python-i18n/blob/main/notebooks/complex_script_support_images.ipynb – Andj Mar 01 '23 at 03:22
  • @fatima your code sample is importing matplotlib.pyplot, matplotlib has an alternative backend mplcairo which uses libraqm as well. Using mplcairo adds complex rendering and bidi support to matplotlib, and by extension seaborn, wordcloud, the default Pandas plot, and other packages built on matplotlib. – Andj Mar 01 '23 at 03:31
  • how install RAQM library – fatima Mar 02 '23 at 09:41
  • how should i install RAQM – fatima Mar 05 '23 at 06:41

1 Answers1

1

Pillow requires Raqm for rendering complex script text. Raqm requires additional software, instructions for installing dependencies and building Raqm are available at https://github.com/HOST-Oman/libraqm.

For Windows look at https://github.com/python-pillow/Pillow/issues/4859 and Installing Raqm (Libraqm) Windows 10

A minimal example, using Raqm:

from PIL import Image, ImageFont, ImageDraw
text = 'اردو'
img = Image.new('RGB', (200, 150))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('~/Library/Fonts/Nafees Nastaleeq.ttf', 50, layout_engine=ImageFont.Layout.RAQM)
draw.text((25,40), text, fill='white', font=font)
img.show()
Andj
  • 481
  • 3
  • 8