好的,首先,我的数据来源是GoogleSheets,一个包含行(ID、自白和年龄)的简单表格 我使用GoogleSheetsAPI,使用gspread和GoogleOAuth2Client将数据连接到python脚本,以获取忏悔栏(大部分是段落,大约100到200个阿拉伯语单词)
一旦数据/自白被存储到代码中的自白变量中,我就通过阿拉伯语重塑函数运行它并获得显示(bidi算法)。然后,“Pillow-Img”对输出进行处理,以便将段落添加到图像中。输出很好,阿拉伯语显示得很完美,但线条从底部开始,而不是从右上方开始
文本必须从右向左书写
然而,实际的段落是
كيما يكون التقريران متسقين تماما، فإن هناك تغييرا طفيفا في الفقرة 2 من تقرير الفريق العامل الثاني، التي يرد فيها ذكر الرئيس والموظفين المعنيين الآخرين.
正如你所注意到的,“كيايون㶈تقي㶈ن”在图像中是在句子的结尾,但在原始段落(引用)中是在开头(右上方)所以基本上,行是颠倒的,我找不到一个解决办法,不把段落分成几行,然后逐行打印,这是不可能的,因为平均段落大约有200字
get_arabic_text()
和arabicfix()
都是为重塑阿拉伯语而构建的函数,但get_arabic_文本分割输入并逐字重塑。但是arabicfix()只是将所有数据推送到阿拉伯语整形器中。在代码中,im仅触发arabicfix()
我倾向于认为问题在bidi algorithm
之内,但我无法找到解决方案
# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw, ImageFont
from bidi.algorithm import get_display
import textwrap, os, re, arabic_reshaper
from rtl import reshaper
def f(p): return os.path.join(directory, p)
directory = os.path.normcase(os.path.dirname(__file__))
def get_arabic_text(text):
if reshaper.has_arabic_letters(text):
words = text.split()
reshaped_words = []
for word in words:
if reshaper.has_arabic_letters(word):
# for reshaping and concating words
reshaped_text = reshaper.reshape(word)
# for right to left
bidi_text = get_display(reshaped_text)
reshaped_words.append(bidi_text)
else:
reshaped_words.append(word)
reshaped_words.reverse()
return ' '.join(reshaped_words)
return text
def draw_multiple_line_text(image, text, font, text_color, text_start_height):
draw = ImageDraw.Draw(image)
print(text)
image_width, image_height = image.size
y_text = text_start_height
lines = textwrap.wrap(text, width=65)
print(lines)
for line in lines:
line_width, line_height = font.getsize(line)
draw.text(((image_width - line_width)/2, y_text),line, font=font, fill=text_color)
y_text += line_height
def arabicfix(text):
config_from_font = arabic_reshaper.config_for_true_type_font(
f('arial.ttf'))
reshaper = arabic_reshaper.ArabicReshaper(config_from_font)
text_to_be_reshaped = text
reshaped_text=reshaper.reshape(text_to_be_reshaped)
bidi_text = get_display(reshaped_text,base_dir='R')
return bidi_text
def main():
image = Image.open('twitter2.jpg')
arabictext=u"كيما يكون التقريران متسقين تماما، فإن هناك تغييرا طفيفا في الفقرة 2 من تقرير الفريق العامل الثاني، التي يرد فيها ذكر الرئيس والموظفين المعنيين الآخرين."
text=arabicfix(arabictext)
print("text:", text)
fontsize = 25
textcolor= (0,0,0)
text_height =50
font = ImageFont.truetype('/Users/Hady/PycharmProjects/untitled/arial.ttf', fontsize)
draw_multiple_line_text(image, text, font,textcolor,text_height)
image.save(f('tofff.jpg'))
main()
目前没有回答
相关问题 更多 >
编程相关推荐