نوشتن متن فارسی بر روی تصویر در پایتون - هفت خط کد انجمن پرسش و پاسخ برنامه نویسی

نوشتن متن فارسی بر روی تصویر در پایتون

0 امتیاز
سلام . من با opencv متن فارسی می نویسم روی تصویر همش علامت سوال رسم می کنه چطور متن فارسی رو به صورت صحیح بر روی تصویر نمایش بدم؟
سوال شده خرداد 28, 1401  بوسیله ی ٍEydi (امتیاز 68)   5 14 16

1 پاسخ

+1 امتیاز
 
بهترین پاسخ

به جای opencv از PIL استفاده کنید.

from PIL import Image, ImageDraw, ImageFont
import arabic_reshaper
from bidi.algorithm import get_display

# Create a new image
img = Image.new('RGB', (500, 500), color='white')

# Create a drawing context
draw = ImageDraw.Draw(img)

# Set the font
font = ImageFont.truetype(r"B Titr Bold_p30download.com.ttf", size=32, encoding='unic')

# Set the text
text = 'این یک متن فارسی است'

# Reshape and reorder the text
reshaped_text = arabic_reshaper.reshape(text)
display_text = get_display(reshaped_text)

# Get the text size
text_size = draw.textsize(display_text, font=font)

# Calculate the position to draw the text
x = (img.width - text_size[0]) / 2
y = (img.height - text_size[1]) / 2

# Draw the text
draw.text((x, y), display_text, fill='black', font=font)

# Save the image
img.save(r'd:\persian_text.png')

 

پاسخ داده شده خرداد 28, 1401 بوسیله ی toopak (امتیاز 2,458)   16 48 66
انتخاب شد شهریور 8, 1402 بوسیله ی farnoosh
...