字体和 unicode - python 中的 PIL
Font and unicode - PIL in python
我想使用 LaTeX 或类似的字体。为什么在这种字体中 Unicode 不起作用?有些字体适合 Unicode 而有些不适合?我应该使用哪种字体?非常感谢
from PIL import Image, ImageDraw, ImageFont, ImageFilter
font_size=36
width=500
height=100
back_ground_color=(255,255,255)
font_size=36
font_color=(0,0,0)
unicode_text = u"\u03B2 Hello"
im = Image.new ( "RGB", (width,height), back_ground_color )
draw = ImageDraw.Draw ( im )
unicode_font = ImageFont.truetype("DejaVuSans.ttf", 40)
font = ImageFont.truetype('lmroman7-regular.otf', 40)
draw.text ( (10,10), unicode_text, font=unicode_font, fill=font_color )
draw.text ( (20,40), unicode_text, font=font, fill=font_color )
im.save("text.jpg")
我的字体文件与 python 代码位于同一目录中。我正在寻找能够显示测试版的 LM Roman 字体下载。
错误是什么?我 运行 你的代码给了我错误 OSError: can not open resource
。如果是这种情况,那么您应该提供字体地址而不是字体名称。 PIL
也可以支持 .oft
字体。但我不确定这个 Unicode (\u03B2)
是否会给你一个矩形。这是您的代码(希望对您有所帮助):
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import os
# __file__ is the path for your code file
directory,filename = os.path.split(__file__)
font_size=36
width=500
height=100
back_ground_color=(255,255,255)
font_size=36
font_color=(0,0,0)
unicode_text = "\u03B2 Hello"
im = Image.new ( "RGB", (width,height), back_ground_color )
draw = ImageDraw.Draw ( im )
# if the font is installed in your computer use this code
unicode_font = ImageFont.truetype("C:\Windows\Fonts\Arial.ttf", 40)
#if it's in the same folder as the code, use this code
font = ImageFont.truetype(directory + "\lmroman10-regular.otf", 40)
draw.text ( (10,10), unicode_text, font=unicode_font, fill=font_color )
draw.text ( (20,40), unicode_text, font=font, fill=font_color )
im.save("text.jpg")
CMU-serif 是一种类似于 LM Roman 的合适字体,可以使用希腊字母
我想使用 LaTeX 或类似的字体。为什么在这种字体中 Unicode 不起作用?有些字体适合 Unicode 而有些不适合?我应该使用哪种字体?非常感谢
from PIL import Image, ImageDraw, ImageFont, ImageFilter
font_size=36
width=500
height=100
back_ground_color=(255,255,255)
font_size=36
font_color=(0,0,0)
unicode_text = u"\u03B2 Hello"
im = Image.new ( "RGB", (width,height), back_ground_color )
draw = ImageDraw.Draw ( im )
unicode_font = ImageFont.truetype("DejaVuSans.ttf", 40)
font = ImageFont.truetype('lmroman7-regular.otf', 40)
draw.text ( (10,10), unicode_text, font=unicode_font, fill=font_color )
draw.text ( (20,40), unicode_text, font=font, fill=font_color )
im.save("text.jpg")
我的字体文件与 python 代码位于同一目录中。我正在寻找能够显示测试版的 LM Roman 字体下载。
错误是什么?我 运行 你的代码给了我错误 OSError: can not open resource
。如果是这种情况,那么您应该提供字体地址而不是字体名称。 PIL
也可以支持 .oft
字体。但我不确定这个 Unicode (\u03B2)
是否会给你一个矩形。这是您的代码(希望对您有所帮助):
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import os
# __file__ is the path for your code file
directory,filename = os.path.split(__file__)
font_size=36
width=500
height=100
back_ground_color=(255,255,255)
font_size=36
font_color=(0,0,0)
unicode_text = "\u03B2 Hello"
im = Image.new ( "RGB", (width,height), back_ground_color )
draw = ImageDraw.Draw ( im )
# if the font is installed in your computer use this code
unicode_font = ImageFont.truetype("C:\Windows\Fonts\Arial.ttf", 40)
#if it's in the same folder as the code, use this code
font = ImageFont.truetype(directory + "\lmroman10-regular.otf", 40)
draw.text ( (10,10), unicode_text, font=unicode_font, fill=font_color )
draw.text ( (20,40), unicode_text, font=font, fill=font_color )
im.save("text.jpg")
CMU-serif 是一种类似于 LM Roman 的合适字体,可以使用希腊字母