返回介绍

ImageFont Module

发布于 2021-01-07 09:04:44 字数 4781 浏览 770 评论 0 收藏 0

The ImageFont module defines a class with the same name. Instances of this class store bitmap fonts, and are used with the PIL.ImageDraw.Draw.text() method.

PIL uses its own font file format to store bitmap fonts. You can use the :command`pilfont` utility to convert BDF and PCF font descriptors (X window font formats) to this format.

Starting with version 1.1.4, PIL can be configured to support TrueType and OpenType fonts (as well as other font formats supported by the FreeType library). For earlier versions, TrueType support is only available as part of the imToolkit package

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.codef", 15)

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

Functions

PIL.ImageFont.load(filename)

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)

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=None, size=10, index=0, encoding='', filename=None)

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()

Load a “becodeer than nothing” default font.

1.1.4 新版功能.

返回:A font object.

Methods

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文