python-3.x - 如何正确使用 `cv2.putText` 在图像上绘制阿拉伯文字? (Python+OpenCV)

标签 python-3.x opencv arabic

我使用 python cv2(window10, python3.6) 在图像中写入文本,当文本为英文时它可以工作,但是当我使用阿拉伯文本时它会在图像中写入乱码。

下面是我的代码:

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()

问题在这里 blank_image = cv2.putText(blank_image, "اللغ٩ العربيو", org, font,
fontScale, color, thickness, cv2.LINE_AA)

Output

最佳答案

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()

关于python-3.x - 如何正确使用 `cv2.putText` 在图像上绘制阿拉伯文字? (Python+OpenCV),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59896297/

相关文章:

jquery 阿拉伯字母支持

python:如何打印 y 作为 x 的函数?

c++ - 将 OpenCV 灰度 Mat 转换为 Caffe blob

ios - 在xcode上反向复制阿拉伯语?

c++ - 如何纠正编译OpenCV时的 "undefined reference"错误?

python - OpenCV 不会在 PyCharm 中导入

c# - 带复数词的阿拉伯语 WordNet

python - Python 中的 for 循环问题中的 Tkinter 按钮

python 正态分布

python-3.x - 使用 plt.plot 与 plt.hist 的彩色图像直方图差异 [Python]