[Python] How can I print Hindi sentences(unicode) on image in Python?

Discussão em 'Python' iniciado por Stack, Setembro 10, 2025.

  1. Stack

    Stack Membro Participativo

    I have a file named "hindi.txt". It has contents as follows. I'm using Python3.5.

    कामकाजी महिलाओं के लिए देश में दिल्ली असुरक्षित, सिक्किम सबसे बेहतर: रिपोर्ट
    9 साल से अटकी राफेल डील मंजूर, 59000 Cr में भारत खरीदेगा 36 फाइटर प्लेन
    WhatsApp को टक्कर देने आर्टिफिशियल इंटेलिजेंस के साथ आया गूगल का Allo मैसेंजर
    उड़ी हमले पर 10 खुलासे: आर्मी बेस में 150 मीटर अंदर तक घुस आए थे जैश के आतंकी
    उड़ी हमलाः भारत का कड़ा रुख देखकर PAK ने LoC से सटे शहरों में कैंसल
    PAK को आतंकी देश करार देने के लिए अमेरिकी संसद में पेश हुआ बिल


    I'm opening this file in and then reading line by line. Then printing this text in image. My code snippet is shown as below.

    from PIL import Image, ImageDraw, ImageFont, ImageOps
    import os

    with open("hindi.txt", "r") as filestream:
    cnum = 1
    astr = filestream.read().splitlines()

    font5 = ImageFont.truetype('/home/SunehraBharat/filestotweet/fonts/ARIALUNI.TTF', 26)

    MAX_W, MAX_H = 1500, 1500


    foreground_image = Image.new('RGB', (MAX_W, MAX_H), (0, 0, 0, 0))
    draw = ImageDraw.Draw(foreground_image)
    image_name = str(cnum) + "_" + "image.png"

    current_h, pad = 40, 14
    c = 1
    for txtline in astr:
    line = str(c) + "). " + txtline
    #printing on console to check if lines are coming correctly.
    print(line)
    w, h = draw.textsize(line, font=font5)
    draw.text((10, current_h), line, font=font5, fill=(255,255,255,1))
    current_h += h + pad
    c = c + 1


    #saving image
    foreground_image.save(image_name)
    cnum = cnum + 1


    Output on console due to print(line) statement- Correct

    कामकाजी महिलाओं के लिए देश में दिल्ली असुरक्षित, सिक्किम सबसे बेहतर: रिपोर्ट
    9 साल से अटकी राफेल डील मंजूर, 59000 Cr में भारत खरीदेगा 36 फाइटर प्लेन
    WhatsApp को टक्कर देने आर्टिफिशियल इंटेलिजेंस के साथ आया गूगल का Allo मैसेंजर
    उड़ी हमले पर 10 खुलासे: आर्मी बेस में 150 मीटर अंदर तक घुस आए थे जैश के आतंकी
    उड़ी हमलाः भारत का कड़ा रुख देखकर PAK ने LoC से सटे शहरों में कैंसल
    PAK को आतंकी देश करार देने के लिए अमेरिकी संसद में पेश हुआ बिल


    Now my Image Output:

    [​IMG] As you can compare now, output is not with respect to input. Few words are incorrect "सिक्किम" , "महिलाओं".

    I have tried different fonts. But getting the same result everytime. Can you please help me. And let me know where I'm missing.

    Continue reading...

Compartilhe esta Página