Introduction to the ImageFont module of Python image processing library PIL

Source: Internet
Author: User
Tags image processing library

Introduction to the ImageFont module of Python image processing library PIL

The ImageFont module defines classes with the same name, that is, the ImageFont class. Instances of this class store bitmap fonts for the text () method of the ImageDraw class.

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

From version 1.1.4, PIL can configure whether to support TrueType and OpenType fonts (the same as the FreeType Library supports other font formats ). For earlier versions, TrueType fonts are only supported in the imToolkit package.

TrueType uses Quadratic B-spline Curves and straight lines in the ry to describe the Shape Contour of the font. It features that TrueType can be used as both a print font and screen display; since it is described by instructions on the font, it is irrelevant to the resolution, and always output according to the resolution of the printer. The characters are always smooth and do not contain any sawtooth characters, regardless of whether the characters are enlarged or reduced. However, the quality of the PostScript font is less than that of the PostScript font. Especially when the text is too small, it is not very clear.

OpenType is also called the Type 2 font. It is another font format developed by Microsoft and Adobe. It is also a contour font, which is more powerful than TrueType. The most obvious advantage is that it can be embedded into TrueType software. It also supports multiple platforms, large character sets, and copyright protection. It can be said that it is a superset of Type 1 and TrueType. Main advantages of OpenType: 1) enhanced cross-platform functions 2) better support for international character sets defined by Unicode Standards 3) support for advanced printing control capability 4) smaller file size 5) supports adding digital signatures to character sets to ensure file integration.

The OpenType standard also defines the suffix of the OpenType file name. The suffix of the OpenType file containing the TureType font is. ttf, And the suffix of the file containing the PostScript font is. OTF. If it is a pack file containing a series of TrueType fonts, the suffix is. TTC.

1. Functions of the ImageFont Module

1. Load

Definition: ImageFont. load (file )? Font instance

Meaning: loads a font from a specified file. This function returns the corresponding font object. If this function fails, an IOError occurs.

2. Load_path

Definition: ImageFont. load_path (file )? Font instance

Meaning: it is the same as the function load (), but if the current path is not specified, the specified font file will be searched from sys. path.

3. Truetype

Definition 1: ImageFont. truetype (file, size )? Font instance

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

In windows, if the specified file does not exist, the loader will check whether the font directory of windows exists.

This function requires the _ imagingft service.

Definition 2: ImageFont. truetype (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 using the specified encoding method. The common encoding methods include "unic" (Unicode), "symb" (Microsoft Symbol), "ADOB" (Adobe Standard), and "ADBE" (Adobe Expert) and "armn" (Apple Roman ).

The following example uses the MicrosoftSymbol font, that is, the encoding variable is "symb", and draws a character between oxF000 and 0xF0FF.

font = ImageFont.truetype("symbol.ttf", 16, encoding="symb")draw.text((0, 0), unichr(0xF000 + 0xAA))

4. Load_default

Definition: ImageFont. load_default ()? Font instance

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

2. ImageFont Module Method

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

1. Getsize

Definition: font. getsize (text )? (Width, height)

Meaning: returns the width and height of the given text. The returned value is a 2-tuple.

2. Getmask

Definition: font. getmask (text, mode = ")? Image object

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

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

(New in 1.1.5) the optional mode parameter is used by some graphics card drivers to specify their preferred mode. If it is null, The Renderer may return any mode. Note: This mode is always a string.

Iii. Examples of the ImageFont Module

>>> From PIL import Image, ImageDraw, ImageFont >>> im02 = Image. open ("D :\\ Code \ Python \ test \ img \ test02.jpg") >>> draw = ImageDraw. draw (im02) >>> ft = ImageFont. truetype ("C :\\ WINDOWS \ Fonts \ SIMYOU. TTF ", 20) >>> draw. text (), u "Python image processing library PIL from beginner to proficient", font = ft, fill = 'red') >>> ft = ImageFont. truetype ("C :\\ WINDOWS \ Fonts \ SIMYOU. TTF ", 40) >>> draw. text (30,100), u "Python image processing library PIL from entry to proficient", font = ft, fill = 'green') >>> ft = ImageFont. truetype ("C :\\ WINDOWS \ Fonts \ SIMYOU. TTF ", 60) >>> draw. text (30,200), u "Python image processing library PIL from entry to proficient", font = ft, fill = 'Blue ') >>> ft = ImageFont. truetype ("C :\\ WINDOWS \ Fonts \ SIMLI. TTF ", 40) >>> draw. text (30,300), u "Python image processing library PIL from entry to proficient", font = ft, fill = 'red') >>> ft = ImageFont. truetype ("C :\\ WINDOWS \ Fonts \ STXINGKA. TTF ", 40) >>> draw. text (30,400), u "Python image processing library PIL from beginner to proficient", font = ft, fill = 'yellow') >>> im02.show ()

In windows, the font file is located in the C: \ Windows \ Fonts folder. In this example, SIMYOU. TTF is used as the young font file, SIMLI. TTF is the simplified font file, and STXINGKA. TTF is the row-based font file. You can select a font file from the Fonts folder as needed.

The font size can be set based on the second parameter when the font is defined.

In this example, the image im02 is shown in:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.