How to draw arabic text on the image using `cv2.putText`correctly? (Python+OpenCV)
Asked Answered
S

3

5

I use python cv2(window10, python3.6) to write text in the image, when the text is English it works, but when I use Arabic text it writes messy code in the image.

Below is my code:

import cv2 
import numpy as np
blank_image = np.zeros((100,300,3), np.uint8)
font = cv2.FONT_HERSHEY_SIMPLEX 
org = (10, 50) 
fontScale = .5
color = (255, 0, 0)  
thickness = 1
blank_image = cv2.putText(blank_image, "اللغة العربية", org, font,  
                   fontScale, color, thickness, cv2.LINE_AA)
window_name = 'Image'
cv2.imshow(window_name, blank_image) 
cv2.waitKey(0)
cv2.destroyAllWindows()

the problem here blank_image = cv2.putText(blank_image, "اللغة العربية", org, font,
fontScale, color, thickness, cv2.LINE_AA)

Output

Succursal answered 24/1, 2020 at 12:19 Comment(4)
Different script, same problem: stackoverflow.com/questions/50854235Ursel
I try it and the output not work .. maybe because of different languageSuccursal
That module was likely only written to support English, and/or certain character codes. It probably uses the lower portion of the ASCII character set, not the whole extended set of unicode characters that were added over time. You'll need to find a module that was written to support the characters/languages you are interested in, or, if you have access to the source, perhaps you can modify it to extend the character set it knows about, or write your own module, or train some module on new data that fits your needs. Also check your character encoding.Wretch
Does this answer your question? How to draw Chinese text on the image using `cv2.putText`correctly? (Python+OpenCV)Wretch
B
7
fontpath = "arial.ttf" # <== download font
font = ImageFont.truetype(fontpath, 32)
img_pil = Image.fromarray(frame)
draw = ImageDraw.Draw(img_pil)
draw.text((50, 80),'عربي', font = font)
img = np.array(img_pil)
cv2.imshow(window_name, img) 
import cv2 
import arabic_reshaper
from bidi.algorithm import get_display
import numpy as np
from PIL import ImageFont, ImageDraw, Image


cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    text = "ذهب الطالب الى المدرسة"
    reshaped_text = arabic_reshaper.reshape(text)
    bidi_text = get_display(reshaped_text) 
    fontpath = "arial.ttf" # <== https://www.freefontspro.com/14454/arial.ttf  
    font = ImageFont.truetype(fontpath, 32)
    img_pil = Image.fromarray(frame)
    draw = ImageDraw.Draw(img_pil)
    draw.text((50, 80),bidi_text, font = font)
    img = np.array(img_pil)

    cv2.imshow('window_name', img) 


    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Boz answered 4/10, 2020 at 19:32 Comment(2)
Please provide an explanation how your code solves the problem. Cheers!Bluebell
Complete the download of libraries before starting the program. تمام قبل تشقيل البرنامج يجب تنزيل المكاتبBoz
E
0
#before opening the programme
#first download arabic_reshaper lib write in Anaconda prompt
#"pip install arabic_reshaper"
#and download bidi lib write in Anaconda prompt 
#"pip install python-bidi"

import arabic_reshaper 
from bidi.algorithm import get_display
import numpy as np
from PIL import ImageFont, ImageDraw, Image
import cv2

img =cv2.imread("sun.jpg")
fontpath = "arial.ttf" # <== download font
font = ImageFont.truetype(fontpath, 32)
img_pil = Image.fromarray(img)
draw = ImageDraw.Draw(img_pil)
text="اللغة العربية"
reshaped_text = arabic_reshaper.reshape(text)
bidi_text = get_display(reshaped_text) 
draw = ImageDraw.Draw(img_pil)
draw.text((50, 80),bidi_text, font = font)
img = np.array(img_pil)
cv2.imshow("image with arabic", img) 
cv2.waitKey(0)
cv2.destroyAllWindows()
Eddie answered 6/6, 2022 at 4:59 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Eagre
C
0
import cv2
import mediapipe as mp
import arabic_reshaper
from bidi.algorithm import get_display
import numpy as np
from PIL import ImageFont, ImageDraw, Image


# Initialize Video Capture
cap = cv2.VideoCapture(0)

# Initialize MediaPipe Hands
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(static_image_mode=False, max_num_hands=2, 
min_detection_confidence=0.7)

while True:
    # Capture the frame
    ret, frame = cap.read()

    # Flip the frame horizontally for a mirror-like effect
    frame = cv2.flip(frame, 0)

    # Convert the image to RGB for use with MediaPipe
    image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    # Process the image with MediaPipe Hands
    results = hands.process(image)

    # Extract the hand order
    if results.multi_handedness:
        for idx, hand_handedness in enumerate(results.multi_handedness):
            handedness_str = hand_handedness.classification[0].label
            if handedness_str == 'Left':
                hand_order = 'دست چپ'
            elif handedness_str == 'Right':
                hand_order = 'دست راست'

        
            reshaped_text = arabic_reshaper.reshape(hand_order)
            bidi_text = get_display(reshaped_text) 
            fontpath = "arial.ttf" # <== 
https://www.freefontspro.com/14454/arial.ttf  
            font = ImageFont.truetype(fontpath, 32)
            img_pil = Image.fromarray(frame)
            draw = ImageDraw.Draw(img_pil)
            draw.text((50, 80),bidi_text, font = font)
            img = np.array(img_pil)
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2, cv2.LINE_AA)

            # Show the frame
            cv2.imshow('Frame', img)

            # Exit if the 'q' key is pressed
            if cv2.waitKey(1) & 0xFF == ord('q'):
            break

# Release the VideoCapture and destroy all windows
cap.release()
cv2.destroyAllWindows()
Caswell answered 20/7, 2023 at 4:30 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Eagre

© 2022 - 2025 — McMap. All rights reserved.