Skip to main content

PIL Text Scaler

Project description

License: GPL v3 PyPI3 PyPI1

PIL Text Scaler

Module for automatically fitting a string of text inside of a specified area.

Usage

Loading a Font

To use a font with this module, it first needs to be loaded. The function loadTTF is used to load a True Type Font file. It takes 4 arguments: the name to be used for the font, the path of the font, an optional encoding, an option to load the fast version of the font, an optional minimum size, and an optional maximum size.

import PTS

PTS.loadTTF('arial', 'arial.ttf')

Scaling Text

To figure out if a string of text will fit in an area with the current settings, all you need to do is use the main function of the module: fitText. The function takes 5 arguments: the text to be fit, the width of the area in pixels, the height of the area in pixels, an optional font name (defaults to “consolas”), an optional minimum size to use (use None to try all available sizes), and an option to use fast fonts, when available. The function returns a tuple of the wrapped text, the font that worked, and the size of said font, in that order.

import PTS

text = "This is a string of text to be fit."

PTS.loadTTF('arial', 'arial.ttf') # Load the font
result = PTS.fitText(text, 100, 500, 'arial', 23, fast = True)

Getting the Minimum and Maximum Text Sizes

If you would like to get information on the sizes that a font is using, you can use the getSize function. It takes a font name and returns a dictionary with size information about that font.

import PTS

information = PTS.getSize('arial')

Changing the Minimum and Maximum Text Sizes

If you would like to change the minimum and maximum text sizes (as well as the difference between each size the module tries) you can use the setSize function. This function will change an existing font or will change the defaults for new fonts if no font has been specified. It takes 4 parameters: minimum size (inclusive), the maximum size (exclusive), an optional step parameter which is used to tell it how for to space each valid size from each other, and the name of a font to change. The default for these values are 15, 35, 2, and None, respectively.

import PTS

minimum = 30
maximum = 60
step = 2

PTS.setSizes(30, 60, 2) # Changes the default for new fonts.
PTS.loadTTF('arial', 'arial.ttf') # Will load using the new sizes.

Fast Fonts

Fast fonts are a way to process the text data much faster. The downsides are that they are memory intensive, taking longer to load, and are slightly less accurate. Fonts that have characters that overlap will be less accurate, sometimes by a large number of pixels.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PTS-1.4.8.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

PTS-1.4.8-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file PTS-1.4.8.tar.gz.

File metadata

  • Download URL: PTS-1.4.8.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for PTS-1.4.8.tar.gz
Algorithm Hash digest
SHA256 e5147f22736dc6bc2f30866ce26f47342faecafb641cef4ad5fa41ac797dea61
MD5 604f760f16ef027c94477a5d84f2f1b0
BLAKE2b-256 b4ae861a2afc4dbb731aa6d6be645d7b13e3f3d750b3e6a6c48c1989374a5e97

See more details on using hashes here.

File details

Details for the file PTS-1.4.8-py3-none-any.whl.

File metadata

  • Download URL: PTS-1.4.8-py3-none-any.whl
  • Upload date:
  • Size: 19.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for PTS-1.4.8-py3-none-any.whl
Algorithm Hash digest
SHA256 d7b6811a6d145273c29d5df8566df356ca291077053d2d6fbb07472bbd290291
MD5 ed56fd34f081c7663ee61f7f55eed08e
BLAKE2b-256 d2ec7fec324626ec59e1345ebd925528bf78260043b16592f4aae1a0f44b6ecf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page