Search code examples
pythonopencvcomputer-visionpython-imaging-library

Putting text on images in python in languages other than english


I am working on a computer vision project and am in its last stage.

I require to display text from languages like Hindi, Telugu , Tamil etc on the images.

I even downloaded the ttf (true type font) but it display the text on the image still as '????' or as boxes. I tried putting the text using both openCv 'putText' method and using PIL draw text function but no help.

Any suggestion or help will be appreciated. Thank You!


Solution

  • With PIL you can do it by specifying the font, Here is a sample that works to display hindi.

    from PIL import Image, ImageDraw, ImageFont
    
    fn = "Image.png"
    
    img = Image.open(fn)
     
    I1 = ImageDraw.Draw(img)
    myFont = ImageFont.truetype('NirmalaB.ttf', 65, layout_engine=ImageFont.Layout.RAQM)
    I1.text((20, 36), "हेलो वर्ल्ड", fill=(0, 255, 0), font=myFont)
    
     
    img.save("text_"+fn)
    

    For this to work you also need to download a font that supports hindi. Here is a font that i have found that works Here