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

标签 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')

结果图像:
enter image description here

以下是使用乌尔都语完成的代码和结果:
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')

结果图像:
enter image description here

我还尝试使用阿拉伯语 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')

以及由此产生的图像:
enter image description here

最佳答案

进一步的研发发现,这仍然是一个专门针对乌尔都语的问题,目前还没有解决方案:
As per described here.

但是,我能够使用旧的 .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);

这里也是结果图像:

enter image description here

此外,它还提供了更多功能,例如获取给定字体和字体大小的文本图像大小以及居中您的内容。
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();

同样在上面的例子中,居中对齐代码如下:
 drawing.DrawString("نیدرلینڈز کی ملکہ پاکستان آ رہی ہیں", font, textBrush, new Rectangle(0, 0, img.Width, img.Height), new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });

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

此外,有关更多详细信息,请参阅文档 here .

关于python - Pillow 无法为不同字体呈现乌尔都语文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58888419/

相关文章:

java - Android 照片上的 EXIF 方向字段为空

java - 如何使用Java API读取图像?

python - 更快地计算图像 (M, N) 和模板 (3, 3) 之间的平方差之和以进行模板匹配?

python - 如何将列表列表的字符串转换为列表?

python - Django - 如何在字段中添加同一模型的多个实例

java - 相机没有返回我拍摄的图像

python - 使用 Python 和 pillow (PIL) 填充空心形状

python - 麻烦与代码-> Python识别报纸上的面孔

python - 如何在 Django ImageField 中验证图像格式

Python多重赋值问题(列表)