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

Related searches

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:

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.

There seems an open bug for rendering the hindi (Devanagari font) text.

https://github.com/python-pillow/Pillow/issues/3191

You may try with some other library like: pyvips (I do not find the API very intuitive, but it may work for you)

import pyvips


# To install 'pyvips' refers to https://pypi.org/project/pyvips/
#  1. Intall libvips shared library from https://jcupitt.github.io/libvips/install.html
#  2. Set the PATH variable.
#  3. run pip install pyvips

def generate_tweet_image():
    cnum = 1
    output_file = "tweet_file.png"
    text = u''
    with open("hindi.txt", "r", encoding='UTF-8') as filestream:
        for l in filestream.readlines():
            text = text + f'{cnum}) {l}'
            cnum += 1

    MAX_W, MAX_H = 1500, 1500

    # See for API https://jcupitt.github.io/pyvips/vimage.html#pyvips.Image.text

    # font file: ARIALUNI.TTF
    image = pyvips.Image.text(text, width=MAX_W, height=MAX_H, font='Arial Unicode MS', dpi=96)
    image.write_to_file(output_file)
    print(f'File Written at : {output_file}')


generate_tweet_image()

Output:

Hope this helps.

Devanagari font not working � Issue #2255 � python-pillow/Pillow , 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", � There are multiple ways we can print the Emojis in Python. Let’s see how to print Emojis with Uniocdes, CLDR names and emoji module.. Using Unicodes: Every emoji has a Unicode associated with it.

Pillow 7.0.0 provides support for rendering complex font with raqm library

To check for support:

>>> from PIL import features
>>> print(features.check("raqm"))
True

If it returns False check if library is installed:

 /sbin/ldconfig -p | grep raqm
    libraqm.so.0 (libc6,x86-64) => /usr/lib/libraqm.so.0
    libraqm.so (libc6,x86-64) => /usr/lib/libraqm.so

To Install raqm in debian based distros: sudo apt-get install libraqm-dev

To use raqm as layout engine add layout_engine option while initializing font:

font = ImageFont.truetype("foo.ttf", size=90, layout_engine=ImageFont.LAYOUT_RAQM)

The above code is tested for: Hindi, Marathi, Gujrati and Telugu fonts.

Understanding unicode in python and writing text in devanagri script , I am printing text written in devnagri unicode to jpg and png all i can see in images (comment) ) to use the lohit font, and the initial phrase, and it seems to work: /how-can-i-print-hindi-sentencesunicode-on-image-in-python� by Jimmy Zhang I once spent a couple of frustrating days at work learning how to properly deal with Unicode strings in Python. During those two days, I ate a lot of snacks — roughly one bag of goldfish per one of these errors encountered, which should be all too familiar to those who program with Python: UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xf0 in position 0: ordinal

Installing Raqm, is the ultimate clean soon,check following steps

One of the following methods can be used for building Raqm:

  1. Raqm depends on the following libraries:

FreeType HarfBuzz FriBiDi

To install dependencies on Fedora:

sudo dnf install freetype-devel harfbuzz-devel fribidi-devel gtk-doc

To install dependencies on Ubuntu:

sudo apt-get install libfreetype6-dev libharfbuzz-dev libfribidi-dev \ gtk-doc-tools

On Mac OS X you can use Homebrew:

`export XML_CATALOG_FILES="/usr/local/etc/xml/catalog" # for the docs`

Once you have the source code and the dependencies, you can proceed to build. To do that, run the customary sequence of commands in the source code directory:

To do that, run the customary sequence of commands in the source code directory (configure file is not found in the package, The key was to run autogen.sh before):

$ ./autogen.sh
$ ./configure
$ make
$ make install

To run the tests:

$ make check

sudo ldconfig This step was needed!

Run the following test script: (Make sure the fonts are installed sudo apt install fonts-indic)

from PIL import Image, ImageFont, ImageDraw

im = Image.new("RGB",(160, 160))
draw = ImageDraw.Draw(im)

font_telugu = ImageFont.truetype("/usr/share/fonts/truetype/fonts-telu-extra/Pothana2000.ttf",50)
text_telugu = "నిత్య"

font_hindi = ImageFont.truetype("/usr/share/fonts/truetype/Gargi/Gargi.ttf",50)
text_hindi = "नित्य"

draw.text((10, 10), text_telugu, font=font_telugu)
draw.text((10, 90), text_hindi, font=font_hindi)
im.show()

  1. install tar file from releases If you downloaded the release tarball, you shouldn’t run ./autogen.sh at all, just run steps ./configure directly.

  2. for ubuntu >=18.04, you can install package directly- The requirements for libraqm are:

libc6   >= 2.14
libfreetype6    >= 2.4.2
libfribidi0 >= 1.0.0
libharfbuzz0b   >= 2.1.1

Install the raqm package Update the package index:

    sudo apt-get update

Install libraqm0 deb package:

sudo apt-get install libraqm0

You can test your installation by:

from PIL import features
print(features.check("raqm"))
# you should get True now

How to read Hindi data in Python, This Unicode HOWTO by Python Software Foundation is a short but to print Devanagari script characters (Hindi characters to be precise). I am trying to print Devanagari text on the image. The text is printing on the image. But, it is not appearing the way it supposed to be. Devanagari is not like ordinary language script. In UTF-8 standard some particular characters when appear together show different character(The new character that formed is not define in UTF-8).

A Guide to Unicode, UTF-8 and Strings in Python, [code]import codecs with codecs.open('hindi.txt', encoding='utf-8') as f: input = f. read() print Devanagari script characters (Hindi characters to be precise). So I searched over google for Devanagari characters unicode list and found this page � Ok. So I wrote this question, and found the answer for it. Hindi text can be read using utf-8 encoding. Here is the example. [code]import codecs with codecs.open('hindi.txt', encoding='utf-8') as f: input = f.read() print input [/code] For a

Strings are one of the most common data types in Python. Image for post different scripts including English, Hindi, Chinese and Japanese, as well as emojis. Examples: Unicode code point for alphabet a is U+0061, emoji 🖐 is U+ 1F590, b'\\u4f60\\u597d'# Print bytes encoded in UTF-8 for this string Since Python 3.0, the language’s str type contains Unicode characters, meaning any string created using "unicode rocks!", 'unicode rocks!', or the triple-quoted string syntax is stored as Unicode. The default encoding for Python source code is UTF-8, so you can simply include a Unicode character in a string literal:

This image shows you that the subject of the sentence is the proper noun Gus and that it has a learn relationship with piano. Navigating the Tree and Subtree. The dependency parse tree has all the properties of a tree. This tree contains information about sentence structure and grammar and can be traversed in different ways to extract

Comments