-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcreate_png_examples.py
executable file
·120 lines (92 loc) · 3.79 KB
/
create_png_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
"""
Reads all font-bin files from the specified `input` directory and writes png images to t
he specified `output` directory. Optionally limiting the characters included to -first-char
(-f) thru -last-char (-l). This is the program I used to create the png font samples in the
documentation.
.. seealso::
- :ref:`Bitmap Font Samples<bitmap-font-samples>`.
Example
^^^^^^^
.. code-block:: console
- create_png_examples.py font_directory png_directory
Usage
^^^^^
.. code-block:: console
usage: create_png_examples.py [-h] input output
Creates png samples of each text font file from the input directoryto the output directory.
positional arguments:
input input directory containing font-bin files
output output directory to create pngs
optional arguments:
-h, --help show this help message and exit
"""
import os
import importlib
import argparse
import png
def create_png(font_file_name, png_file_name):
"""
Convert image file to python module for use with bitmap method.
Args:
image_file (str): Name of file containing image to convert.
bits_per_pixel (int): The number of bits to use per pixel (1..8).
"""
module_spec = importlib.util.spec_from_file_location("font", font_file_name)
font = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(font)
char_count = font.LAST - font.FIRST
column_count = 16
row_count = char_count // column_count
with open(png_file_name, "wb") as png_file:
image = png.Writer(
(16 + 2) * font.WIDTH, (row_count + 3) * font.HEIGHT, bitdepth=1
)
image_data = [
[0 for _ in range((16 + 2) * font.WIDTH)]
for _ in range((row_count + 3) * font.HEIGHT)
]
#font_count = len(font.FONT) + 1
for chart_row in range(row_count + 2):
for chart_col in range(16):
chart_idx = chart_row * 16 + chart_col
for char_line in range(font.HEIGHT):
for char_byte in range(font.WIDTH // 8):
ch_idx = (
chart_idx * font.HEIGHT * font.WIDTH // 8
+ char_byte
+ char_line * font.WIDTH // 8
)
print(chart_idx, char_count)
data = font.FONT[ch_idx] if chart_idx <= char_count else 0
for bit in range(8):
png_row = (chart_row + 1) * font.HEIGHT + char_line
png_col = (chart_col + 1) * font.WIDTH + char_byte * 8 + bit
image_data[png_row][png_col] = 1 if data & 1 << 7 - bit else 0
print("Creating", png_file_name)
image.write(png_file, image_data)
def main():
"""
Creates PNG samples of each text font file from the input directory to the output directory.
Args:
input (str): Input directory containing font-bin files.
output (str): Output directory to create PNGs.
"""
parser = argparse.ArgumentParser(
description=(
"Creates png samples of each text font file from the input directory"
"to the output directory."
)
)
parser.add_argument("input", help="input directory containing font-bin files")
parser.add_argument("output", help="output directory to create pngs")
args = parser.parse_args()
for file_name in os.listdir(args.input):
if file_name.endswith(".py"):
font_file_name = os.path.join(args.input, file_name)
png_file_name = os.path.join(
args.output, f"{os.path.splitext(file_name)[0]}.png"
)
create_png(font_file_name, png_file_name)
if __name__ == "__main__":
main()