Poo*_*oja 18 python unicode python-imaging-library hindi pillow
我有一个名为"hindi.txt"的文件.它的内容如下.我正在使用Python3.5.
??????? ??????? ?? ??? ??? ??? ?????? ?????????, ??????? ???? ?????: ???????
9 ??? ?? ???? ????? ??? ?????, 59000 Cr ??? ???? ??????? 36 ????? ?????
WhatsApp ?? ????? ???? ??????????? ?????????? ?? ??? ??? ???? ?? Allo ???????
???? ???? ?? 10 ??????: ????? ??? ??? 150 ???? ???? ?? ??? ?? ?? ??? ?? ?????
???? ????? ???? ?? ???? ??? ????? PAK ?? LoC ?? ??? ????? ??? ?????
PAK ?? ????? ??? ???? ???? ?? ??? ??????? ???? ??? ??? ??? ???
Run Code Online (Sandbox Code Playgroud)
我打开这个文件,然后逐行阅读.然后在图像中打印此文本.我的代码段如下所示.
from PIL import Image, ImageDraw, ImageFont, ImageOps
import os
with open("hindi.txt", "r") as filestream:
cnum = 1
astr = filestream.read().splitlines()
font5 = ImageFont.truetype('/home/SunehraBharat/filestotweet/fonts/ARIALUNI.TTF', 26)
MAX_W, MAX_H = 1500, 1500
foreground_image = Image.new('RGB', (MAX_W, MAX_H), (0, 0, 0, 0))
draw = ImageDraw.Draw(foreground_image)
image_name = str(cnum) + "_" + "image.png"
current_h, pad = 40, 14
c = 1
for txtline in astr:
line = str(c) + "). " + txtline
#printing on console to check if lines are coming correctly.
print(line)
w, h = draw.textsize(line, font=font5)
draw.text((10, current_h), line, font=font5, fill=(255,255,255,1))
current_h += h + pad
c = c + 1
#saving image
foreground_image.save(image_name)
cnum = cnum + 1
Run Code Online (Sandbox Code Playgroud)
由于打印(行)声明而在控制台上输出 - 正确
??????? ??????? ?? ??? ??? ??? ?????? ?????????, ??????? ???? ?????: ???????
9 ??? ?? ???? ????? ??? ?????, 59000 Cr ??? ???? ??????? 36 ????? ?????
WhatsApp ?? ????? ???? ??????????? ?????????? ?? ??? ??? ???? ?? Allo ???????
???? ???? ?? 10 ??????: ????? ??? ??? 150 ???? ???? ?? ??? ?? ?? ??? ?? ?????
???? ????? ???? ?? ???? ??? ????? PAK ?? LoC ?? ??? ????? ??? ?????
PAK ?? ????? ??? ???? ???? ?? ??? ??????? ???? ??? ??? ??? ???
Run Code Online (Sandbox Code Playgroud)
现在我的图像输出:
正如您现在可以比较的那样,输出与输入无关.几句话不正确"सिक्किम","महिलाओं".
我尝试过不同的字体.但每次都得到相同的结果.你能帮我么.让我知道我失踪的地方.
ska*_*dya 13
渲染印地文(梵文字体)文本似乎是一个开放的错误.
https://github.com/python-pillow/Pillow/issues/3191
您可以尝试使用其他库:pyvips(我发现API非常直观,但它可能适合您)
import pyvips
# To install 'pyvips' refers to https://pypi.org/project/pyvips/
# 1. Intall libvips shared library from https://jcupitt.github.io/libvips/install.html
# 2. Set the PATH variable.
# 3. run pip install pyvips
def generate_tweet_image():
cnum = 1
output_file = "tweet_file.png"
text = u''
with open("hindi.txt", "r", encoding='UTF-8') as filestream:
for l in filestream.readlines():
text = text + f'{cnum}) {l}'
cnum += 1
MAX_W, MAX_H = 1500, 1500
# See for API https://jcupitt.github.io/pyvips/vimage.html#pyvips.Image.text
# font file: ARIALUNI.TTF
image = pyvips.Image.text(text, width=MAX_W, height=MAX_H, font='Arial Unicode MS', dpi=96)
image.write_to_file(output_file)
print(f'File Written at : {output_file}')
generate_tweet_image()
Run Code Online (Sandbox Code Playgroud)
输出:
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
1630 次 |
| 最近记录: |