SoFunction
Updated on 2024-11-19

Python image processing library PIL's ImageFont module use introduction

The ImageFont module defines a class with the same name, the ImageFont class. Instances of this class store bitmap fonts for use in the text() method of the ImageDraw class.

PIL uses its own font file format to store bitmap fonts. Users can use the pilfont toolkit to convert BDF and PCF font descriptors (Xwindow font format) to this format.

Starting with version 1.1.4, PIL can be configured to support TrueType and OpenType fonts or not (in the same way that the FreeType library supports other font formats). For earlier versions, TrueType fonts are only supported in the imToolkit package.

TrueType using geometry in the quadratic B spline curve and a straight line to describe the outline of the font, which is characterized by: TrueType can be used as both print fonts, but also can be used as a screen display; because it is described by the instructions on the glyphs, so it has nothing to do with the resolution of the output is always in accordance with the resolution of the printer output. Whether zoomed in or out, the characters are always smooth and will not appear jagged. However, compared to PostScript fonts, its quality is a bit worse. Especially when the text is too small, it is not very clear.

OpenType, also known as Type 2 fonts, is another font format developed by Microsoft and Adobe. It is also an outline font, more powerful than TrueType, and one of the most obvious benefits is the ability to embed PostScript fonts into TrueType software. It also supports multiple platforms, a large character set, and copyright protection. It can be said that it is Type 1 and TrueType superset. the main advantages of OpenType: 1) enhanced cross-platform functionality 2) better support for the Unicode standard definition of the international character set 3) support for advanced printing control capabilities 4) generate smaller file sizes 5) support for the character set to add a digital signature, to ensure that the document's integrated features.

The OpenType standard also defines suffixes for OpenType file names. OpenType files containing TureType fonts have a .ttf suffix, and files containing PostScript fonts have a .OTF suffix.In the case of a font package file containing a series of TrueType fonts, the suffix is .TTC.

I. Functions of the ImageFont module

1、  Load

Definition: (file)⇒ Font instance

Meaning: Load a font from a specified file, this function returns the corresponding font object. If the function fails, an IOError exception is thrown.

2、  Load_path

Definition: ImageFont.load_path(file) ⇒ Font instance

Meaning: Same as load(), but if no current path is specified, it will start searching for the specified font file.

3、  Truetype

Definition 1: (file,size)⇒ Font instance

Meaning 1: Loads a TrueType or OpenType font file and creates a font object. This function loads a font object from the specified file and creates a font object for the specified size.

In windows, if the specified file doesn't exist, the loader will by the way see if it exists in the windows font directory.

This function requires the _imagingft service.

Definition 2: (file,size, encoding=value) ⇒ Font instance

Meaning 2: (New in 1.1.5) Load a TrueType or OpenType font file and create a font object with the specified encoding. The usual encodings are "unic" (Unicode), "symb" (Microsoft Symbol), "ADOB" ( ADOB" (Adobe Standard), "ADBE" (Adobe Expert) and "armn" (Apple Roman).

The following example uses the MicrosoftSymbol font, i.e. the variable encoding is "symb", to draw a character between oxF000 and 0xF0FF.

font = ("", 16, encoding="symb")
((0, 0), unichr(0xF000 + 0xAA))

4、  Load_default

Definition: ImageFont.load_default() ⇒ Font instance

Meaning: (New in 1.1.4) Load a default font.

II. Methods of the ImageFont module

The Font object must implement the following methods for use by the ImageDraw layer.

1、  Getsize

Definition: (text)⇒ (width, height)

Meaning: return the width and height of the given text, the return value is a 2-tuple.

2、  Getmask

Definition: (text,mode="")⇒ Image object

Meaning: Returns a bitmap for the given text. This bitmap is an instance of the PIL's internal storage memory (defined for the interface module).

If the font uses anti-aliasing, the mode of the bitmap is "L" and its maximum value is 255; otherwise, its mode is "1".

(New in 1.1.5) The optional parameter mode is used by some graphics drivers to specify their preferred mode; if null, the renderer may return any mode. Note: the mode is always a string.

III. Examples of ImageFont modules

>>> from PIL import Image, ImageDraw, ImageFont
>>> im02 = ("D:\\Code\\Python\\test\\img\\")
>>> draw = (im02)
>>> ft = ("C:\\WINDOWS\\Fonts\\", 20)
>>> ((30,30), u"Python Image Processing Library PIL from Start to Finish",font = ft, fill = 'red')
>>> ft = ("C:\\WINDOWS\\Fonts\\", 40)
>>> ((30,100), u"Python Image Processing Library PIL from Start to Finish",font = ft, fill = 'green')
>>> ft = ("C:\\WINDOWS\\Fonts\\", 60)
>>> ((30,200), u"Python Image Processing Library PIL from Start to Finish",font = ft, fill = 'blue')
>>> ft = ("C:\\WINDOWS\\Fonts\\", 40)
>>> ((30,300), u"Python Image Processing Library PIL from Start to Finish",font = ft, fill = 'red')
>>> ft = ("C:\\WINDOWS\\Fonts\\", 40)
>>> ((30,400), u"Python Image Processing Library PIL from Start to Finish",font = ft, fill = 'yellow')
>>> ()

In windows system, font files are located in C:\Windows\Fonts folder. In this example, the font file used is the young round font file, the official script font file, and the regular script font file. Users can select the required font files from the Fonts folder according to their needs.

The size of the font can be set according to the second parameter when defining the font.

The image im02 in this example is shown below:

to this article on the Python image processing library PIL ImageFont module to introduce the use of the article is introduced to this, more related to the content of the PIL ImageFont module, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!