gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:28:24 45 4
gpt4 key购买 nike

我使用 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/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com