Skip to main content

Generate a C font file for a small LCD from a PNG image

Project description

Hexafonter

A command-line tool that will convert a PNG file with a fixed-width pixel font for small LCD displays, like this:

(sample font image)

to a C file with a sideways preview and a character code legend, like this:

(sample output screenshot)

See sample-output.c for the full thing -- but note that you need a relatively modern monospace font for this to look good.

Installation

You need Python 3.6+ and the png module.

In a Python virtual environent, you can install the tool as:

python -m pip install hexafonter

Usage

To convert a PNG to a C font file:

python -m hexafonter font.png > output-font.c

By default, the tool expects characters to be 8 pixels tall 6 pixels wide. The height is hard-coded, but you can change the width by adding a number:

python -m hexafonter arcade-font.png 8 > output-font.c

Several options let you select a preamble, which should contain the C variable declaration (up to =), and any #include or #define directives or other code you need before it. The default preamble is:

const unsigned char font[] =

You can customize it with these options:

  • -p preamble-file: Load a preamble from the given file
  • -p -: Use no preamble (useful if you prepend it some other way)
  • -P ...: Use the ... directly
  • -progmem: Use a preamble for Arduino-style PROGMEM

Other options:

  • -o outfile.c: Output to the given file, rather than stdout (i.e. don't combine this with the > outfile part shown above)
  • -r: Reverse mode -- convert a C file to a PNG instead. (May not work with files generated by other tools. Details not present in the C file will be lost. On the other hand, the resulting PNG will be relatively small.)
  • -h: Get help

Details of operation

The tool looks at the red channel of each pixel and does a very basic treshold operation. That means:

  • Don't use (semi-)transparent images. (The tool does not “see” what you see in the transparent regions.)
  • Dark colors, especially green/cyan/blue, are treated as black (on).
  • Light colors, especially red/yellow/magenta, and all are treated as white (off).

The preview in the C file uses Unicode BLOCK SEXTANT characters.

License

The tool is provided under the MIT license. May it serve you well.

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

hexafonter-0.1.1.tar.gz (84.3 kB view hashes)

Uploaded Source

Built Distribution

hexafonter-0.1.1-py3-none-any.whl (6.0 kB view hashes)

Uploaded Python 3

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