我怎样才能在枕头库中输入波斯语文本
how can I type persian text in pillow library
如何借助 Python 中的 Pillow Library 在照片上写波斯语文字?
当我尝试写文字时,单词的字母是从末尾写到开头的。
比如我想用波斯语写“Hello World”,照片上就变成了“dolW olleH”。
枕头波斯语书写问题:
from PIL import Image, ImageDraw, ImageFont
a = Image.open('1.jpg')
draw = ImageDraw.Draw(a)
font = ImageFont.truetype('Dana.ttf', size=200)
draw.text((100, 200), 'سلام دنیا', font=font, fill=(255, 255, 255))
参见this。它会帮助你在 Arabic/Persian in Pillow.
中写作
# Tested on Python 3.6.1
# install: pip install --upgrade arabic-reshaper
import arabic_reshaper
# install: pip install python-bidi
from bidi.algorithm import get_display
# install: pip install Pillow
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
# use a good font!
fontFile = "/Users/amirreza/pil/Sahel.ttf"
# this was a 400x400 jpg file
imageFile = "/Users/amirreza/pil/input.jpg"
# load the font and image
font = ImageFont.truetype(fontFile, 18)
image = Image.open(imageFile)
# first you must prepare your text (you dont need this step for english text)
text = "سلام ایران"
reshaped_text = arabic_reshaper.reshape(text) # correct its shape
bidi_text = get_display(reshaped_text) # correct its direction
# start drawing on image
draw = ImageDraw.Draw(image)
draw.text((0, 0), bidi_text, (255,255,255), font=font)
draw = ImageDraw.Draw(image)
# save it
image.save("output.png")
如何借助 Python 中的 Pillow Library 在照片上写波斯语文字? 当我尝试写文字时,单词的字母是从末尾写到开头的。
比如我想用波斯语写“Hello World”,照片上就变成了“dolW olleH”。
枕头波斯语书写问题:
from PIL import Image, ImageDraw, ImageFont
a = Image.open('1.jpg')
draw = ImageDraw.Draw(a)
font = ImageFont.truetype('Dana.ttf', size=200)
draw.text((100, 200), 'سلام دنیا', font=font, fill=(255, 255, 255))
参见this。它会帮助你在 Arabic/Persian in Pillow.
中写作# Tested on Python 3.6.1
# install: pip install --upgrade arabic-reshaper
import arabic_reshaper
# install: pip install python-bidi
from bidi.algorithm import get_display
# install: pip install Pillow
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
# use a good font!
fontFile = "/Users/amirreza/pil/Sahel.ttf"
# this was a 400x400 jpg file
imageFile = "/Users/amirreza/pil/input.jpg"
# load the font and image
font = ImageFont.truetype(fontFile, 18)
image = Image.open(imageFile)
# first you must prepare your text (you dont need this step for english text)
text = "سلام ایران"
reshaped_text = arabic_reshaper.reshape(text) # correct its shape
bidi_text = get_display(reshaped_text) # correct its direction
# start drawing on image
draw = ImageDraw.Draw(image)
draw.text((0, 0), bidi_text, (255,255,255), font=font)
draw = ImageDraw.Draw(image)
# save it
image.save("output.png")