ImageFont Module

ImageFont定义了一个相同名字的类。这个类的实例可以存储点阵字体,储存的点阵字体可以用于 PIL.ImageDraw.Draw.text() 方法。

PIL用它自己的字体文件格式来存储点阵字体。你可用命令 `pilfont` utility  来把BDF和PCF字体描述符 (X window font formats)给转换成PIL的格式。

从1.1.4版本开始,PIL可以支持 TrueType和 OpenType 字体了(就像被这个自由字体库所支持的其他字体格式一样)。

Example

from PIL import ImageFont, ImageDraw

draw = ImageDraw.Draw(image)

# use a bitmap font
font = ImageFont.load("arial.pil")

draw.text((10, 10), "hello", font=font)

# use a truetype font
font = ImageFont.truetype("arial.ttf", 15)

draw.text((10, 25), "world", font=font

   
   

Functions

PIL.ImageFont. load ( filename ) [source]

Load a font file. This function loads a font object from the given bitmap font file, and returns the corresponding font object.

参数:filename – Name of font file.
返回:A font object.
引发 IOError:If the file could not be read.
PIL.ImageFont. load_path ( filename ) [source]

Load font file. Same as load(), but searches for a bitmap font along the Python path.

参数:filename – Name of font file.
返回:A font object.
引发 IOError:If the file could not be read.
PIL.ImageFont. truetype ( font=Nonesize=10index=0encoding=''filename=None ) [source]

Load a TrueType or OpenType font file, and create a font object. This function loads a font object from the given file, and creates a font object for a font of the given size.

This function requires the _imagingft service.

参数:
  • filename – A truetype font file. Under Windows, if the file is not found in this filename, the loader also looks in Windows fonts/ directory.
  • size – The requested size, in points.
  • index – Which font face to load (default is first available face).
  • encoding – Which font encoding to use (default is Unicode). Common encodings are “unic” (Unicode), “symb” (Microsoft Symbol), “ADOB” (Adobe Standard), “ADBE” (Adobe Expert), and “armn” (Apple Roman). See the FreeType documentation for more information.
返回:

A font object.

引发 IOError:

If the file could not be read.

PIL.ImageFont. load_default ( ) [source]

Load a “better than nothing” default font.

1.1.4 新版功能.

返回:A font object.

Methods

PIL.ImageFont.ImageFont. getsize ( text )
返回:(width, height)
PIL.ImageFont.ImageFont. getmask ( textmode='' )

Create a bitmap for the text.

If the font uses antialiasing, the bitmap should have mode “L” and use a maximum value of 255. Otherwise, it should have mode “1”.

参数:
  • text – Text to render.
  • mode –

    Used by some graphics drivers to indicate what mode the driver prefers; if empty, the renderer may return either mode. Note that the mode is always a string, to simplify C-level implementations.

    1.1.5 新版功能.

返回:

An internal PIL storage memory instance as defined by the PIL.Image.core interface module.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值