Home > other >  How to draw different language text on the image?
How to draw different language text on the image?

Time:06-07

I am doing a project where I need to translate the text into other Indian languages

I have tried few codes but the result I get for languages other than english is empty boxes

Original image:

original image

this is the variable output

[([[217, 25], [1061, 25], [1061, 61], [217, 61]],
  'He was in his bedroom: He smelled food, The food smelled',
  0.7254911849154941),
 ([[181, 70], [1006, 70], [1006, 114], [181, 114]],
  'like chicken: It smelled good. A neighbor must be cooking',
  0.773367181175143),

I am using the output coordinates for drawing a rectangle around the text

1 - first code

image = cv2.imread("/content/drive/MyDrive/image_text_3.jpg")

color = (0, 0, 255)
  
# Line thickness
thickness = 1

fontpath = "/content/VANAVIL-AvvaiyarRegular.otf"     
TEXT_Font = ImageFont.truetype(fontpath, 20)

TEXT_SCALE = 0.85
TEXT_THICKNESS = 1

TEXT = "இனிய பிறந்தநாள் வாழ்த்துக்கள் யோஷினி குட்டி"

for bound in output:
    tl, tr, br, bl = bound[0]

    tl = (int(tl[0]), int(tl[1]))
    tr = (int(tr[0]), int(tr[1]))
    br = (int(br[0]), int(br[1]))
    bl = (int(bl[0]), int(bl[1]))

    cv2.rectangle(image, tl, br, color, thickness)
    
    text_size, _ = cv2.getTextSize(TEXT, TEXT_Font, TEXT_THICKNESS)

    text_origin = (tl[0] - text_size[0] / 2, br[1]   text_size[1] / 2)

    cv2.putText(image, TEXT, (tl[0], br[1]), TEXT_Font, TEXT_SCALE, (0,255,0), TEXT_THICKNESS, cv2.LINE_AA)
 
cv2.imwrite('centertext_out.png', image)

This is the error I get for the above code for tamil language

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-103-1d8c09103798> in <module>()
         26     cv2.rectangle(image, tl, br, color, thickness)
         27 
    ---> 28     text_size, _ = cv2.getTextSize(TEXT, TEXT_SCALE, TEXT_THICKNESS)
         29 
         30     text_origin = (tl[0] - text_size[0] / 2, br[1]   text_size[1] / 2)
    
    TypeError: an integer is required (got type FreeTypeFont)

2 - second code for marathi

fontpath = "/content/Devnew.ttf"
img=cv2.imread('/content/drive/MyDrive/image_text_3.jpg')
b,g,r,a = 0,255,0,0
font = ImageFont.truetype(fontpath, 32)
img_pil = Image.fromarray(img)
draw = ImageDraw.Draw(img_pil)
draw.text((50, 80),  "जन्मदिन मुबारक", font = font, fill = (b, g, r, a))
img = np.array(img_pil)

cv2.imwrite('result.png', img)

Result of code 2:

result of code 2

3 - Third code

image = cv2.imread("/content/drive/MyDrive/image_text_3.jpg")

TEXT = "जन्मदिन मुबारक हो योशिनी कुट्टी"
color = (0, 0, 255)



# Line thickness
thickness = 1
b,g,r,a = 0,255,0,0

TEXT_SCALE = 0.9
TEXT_THICKNESS = 1

fontpath = "/content/drive/MyDrive/Devnew.ttf"
font = ImageFont.truetype(fontpath, 20)
img_pil = Image.fromarray(image)
draw = ImageDraw.Draw(img_pil)
draw.text((50, 80), TEXT, font = font, fill =(b,g,r,a))
image = np.array(img_pil)


cv2_imshow(image)

Result of code 3:

result of code 3

I don't understand whether there is a problm with the font file or the code or the image. kindly help in resolving. Thankyou!

My main concern is to put the text inside the rectangle box created in the initial step({cv2.rectangle(image, tl, br, color, thickness} from code 1))

CodePudding user response:

Most likely the problem is with the .otf and .ttf files you are using. Try using the default lohit-tamil fonts from /usr/share/fonts/truetype. They can be installed with sudo apt install fonts-indic if they are not installed already. Use Unicode fonts only or download other Unicode fonts online - e.g. tamil.jpg

  • Related