Pillow 无法为不同字体呈现乌尔都语文本

Tay*_*yab 5 python image-processing python-imaging-library

我正在尝试使用 Pillow 为一些乌尔都语文本生成图像。使用相同的代码生成普通英语就像一个魅力,但是当我对乌尔都语文本做同样的事情时,事情就不会那么顺利了。

以下是用英语完成的代码和结果:

from PIL import Image, ImageFont, ImageDraw

from matplotlib import pyplot as plt
import numpy as np
from bidi.algorithm import get_display

text_string = u'Hello how are you doing?'

img = Image.new('RGB', (720, 480))
draw = ImageDraw.Draw(img)

draw.text((25,40), text_string, fill='white')

img.save('pil_text_font.png')
Run Code Online (Sandbox Code Playgroud)

结果图像: 在此处输入图片说明

以下是使用乌尔都语完成的代码和结果:

from PIL import Image, ImageFont, ImageDraw

from matplotlib import pyplot as plt
import numpy as np
from bidi.algorithm import get_display

text_string = u'????????? ?? ???? ??????? ?? ??? ???'

img = Image.new('RGB', (720, 480))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('../UrduFontsDirectory' + data.iloc[14]['Path'], 25)

draw.text((25,40), text_string, fill='white', font=font)

img.save('pil_text_font.png')
Run Code Online (Sandbox Code Playgroud)

结果图像: 在此处输入图片说明

我还尝试使用阿拉伯语 Reshaper 解决了渲染问题的对齐问题,但有些字符从未被渲染:

from PIL import Image, ImageFont, ImageDraw

from matplotlib import pyplot as plt
import numpy as np
from bidi.algorithm import get_display
from arabic_reshaper import reshape

text_string = u'????????? ?? ???? ??????? ?? ??? ???'
text_string = get_display(reshape(text_string))

img = Image.new('RGB', (720, 480))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('../UrduFontsDirectory' + data.iloc[14]['Path'], 25)

draw.text((25,40), text_string, fill='white', font=font)

img.save('pil_text_font.png')
Run Code Online (Sandbox Code Playgroud)

以及由此产生的图像: 在此处输入图片说明

Tay*_*yab 5

进一步的研发发现,这仍然是乌尔都语特有的问题,目前还没有解决方案:如此处 所述。

但是,我能够使用旧的 .NET 找到解决此问题的方法。对于可能面临类似问题的人,可以使用以下方法生成乌尔都语文本图像(支持所有字体):

//Create the font using your font file    
var modernFont = new PrivateFontCollection();
modernFont.AddFontFile("NafeesNaskh.ttf");//Load font from the font file

var font= new Font(modernFont.Families[0], 12);//The second argument is the font size

//first, create a dummy bitmap just to get a graphics object
Image img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);

//free up the dummy image and old graphics object
img.Dispose();
drawing.Dispose();

//create a new image of the right size
img = new Bitmap(500, 40);

drawing = Graphics.FromImage(img);

//paint the background
drawing.Clear(Color.Black);

//create a brush for the text
Brush textBrush = new SolidBrush(Color.White);

drawing.DrawString("????????? ?? ???? ??????? ?? ??? ???", font, textBrush, new Rectangle(0, 0, img.Width, img.Height), new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
drawing.Save();

textBrush.Dispose();
drawing.Dispose();
string path = "./" + Id[i] + "_" + font.Name + ".jpg";
img.Save(path);
Run Code Online (Sandbox Code Playgroud)

这里也是结果图像:

在此处输入图片说明

此外,它还提供了更多功能,例如获取给定字体和字体大小的文本图像大小以及将内容居中。

Image img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);
//Size of image for current text, font and font size
SizeF textSize = drawing.MeasureString("????????? ?? ???? ??????? ?? ??? ???", font);
img.Dispose();
drawing.Dispose();
Run Code Online (Sandbox Code Playgroud)

同样在上面的例子中,居中对齐代码如下:

 drawing.DrawString("????????? ?? ???? ??????? ?? ??? ???", font, textBrush, new Rectangle(0, 0, img.Width, img.Height), new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
Run Code Online (Sandbox Code Playgroud)

您可以根据需要进行修改以使文本左、右、上或下对齐。

此外,有关更多详细信息,请参阅此处的文档。