fonteditor/exporter.py
2024-02-29 22:12:14 +01:00

69 lines
1.7 KiB
Python

from tkinter import filedialog
from PIL import Image
import numpy as np
def export(char_res, chars):
export_path = filedialog.askdirectory(
title="Choose folder where exported pages will be saved."
)
if export_path == "":
print("\nExport canceled.\n")
return
char_res = np.array(char_res, dtype=np.uint16)
chars = tuple(chars.items())
# there will be 256 characters per page
last_char_idx = len(chars) - 1
page_res = char_res * 16
page = 0
page_char_x = 0
page_char_y = 0
for char_idx, char in enumerate(chars):
if page_char_x == 0 and page_char_y == 0:
page_arr = np.zeros(page_res, dtype=bool)
char_map_string = ""
char_string, char_bitmap = char
# put char_bitmap onto page_img at correct position
page_arr[
page_char_x * char_res[0] : (page_char_x + 1) * char_res[0],
page_char_y * char_res[1] : (page_char_y + 1) * char_res[1]
] = char_bitmap
char_map_string += char_string
page_char_x += 1
if page_char_x == 16:
page_char_x = 0
page_char_y += 1
if page_char_y == 16 or char_idx == last_char_idx:
## save page
# numpy array to 1 bit image
page_img = Image.fromarray(page_arr.T * 255).convert("1")
# save page
page_img.save(f"{export_path}/page_{page}.png")
# save char map
with open(f"{export_path}/page_{page}.txt", "w", encoding="utf-32") as f:
f.write(char_map_string)
page += 1
page_char_y = 0
print("\nExport finished.\n")