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:
to a C file with a sideways preview and a character code legend, like this:
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for hexafonter-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1717c39724fa362bacfd1dd21848436eecf4c84e3c721a2ad8c0bf030a74ca68 |
|
MD5 | ea0b79b787311a1fa9c9037aad97225d |
|
BLAKE2b-256 | 4442ce51baf9f50541ca957e0d39c4954d2f98ebfb5ea97d9d24033faa83eb54 |