Create render_images.py
Browse files- render_images.py +88 -0
render_images.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import xml.etree.ElementTree as ET
|
| 2 |
+
import cairosvg
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def batch_render_kanji(xml_file_path, output_dir="kanji_images_rendered", size=256, bg_color="white"):
|
| 7 |
+
"""
|
| 8 |
+
Parses a full KanjiVG XML file and renders every kanji to a PNG image
|
| 9 |
+
with a specified background color.
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
xml_file_path (str or Path): Path to the kanjivg XML file.
|
| 13 |
+
output_dir (str or Path): Directory where the PNG images will be saved.
|
| 14 |
+
size (int): The width and height of the output PNGs in pixels.
|
| 15 |
+
bg_color (str): The background color for the image (e.g., 'white', '#FFFFFF').
|
| 16 |
+
Set to None for a transparent background.
|
| 17 |
+
"""
|
| 18 |
+
output_path = Path(output_dir)
|
| 19 |
+
output_path.mkdir(exist_ok=True)
|
| 20 |
+
print(f"Output directory set to: {output_path.resolve()}")
|
| 21 |
+
if bg_color:
|
| 22 |
+
print(f"Background color set to: '{bg_color}'")
|
| 23 |
+
else:
|
| 24 |
+
print("Background is set to transparent.")
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
print(f"Loading and parsing XML file: {xml_file_path}...")
|
| 28 |
+
tree = ET.parse(xml_file_path)
|
| 29 |
+
root = tree.getroot()
|
| 30 |
+
except ET.ParseError as e:
|
| 31 |
+
print(f"Fatal Error: Could not parse XML file. Details: {e}")
|
| 32 |
+
return
|
| 33 |
+
except FileNotFoundError:
|
| 34 |
+
print(f"Fatal Error: The file was not found at '{xml_file_path}'")
|
| 35 |
+
return
|
| 36 |
+
|
| 37 |
+
kanji_elements = root.findall('.//kanji')
|
| 38 |
+
total_kanji = len(kanji_elements)
|
| 39 |
+
print(f"Found {total_kanji} kanji elements to render.")
|
| 40 |
+
|
| 41 |
+
if total_kanji == 0:
|
| 42 |
+
print("No kanji elements were found. Please check the XML file content.")
|
| 43 |
+
return
|
| 44 |
+
|
| 45 |
+
for i, kanji_element in enumerate(kanji_elements):
|
| 46 |
+
kanji_id = kanji_element.attrib.get('id', f'unknown_{i}')
|
| 47 |
+
hex_codepoint = kanji_id.split('_')[-1]
|
| 48 |
+
|
| 49 |
+
g_element = kanji_element.find('{http://kanjivg.tagaini.net}g') or kanji_element.find('g')
|
| 50 |
+
|
| 51 |
+
if g_element is None:
|
| 52 |
+
print(f"[{i+1}/{total_kanji}] Skipping {kanji_id}: No drawable <g> element found.")
|
| 53 |
+
continue
|
| 54 |
+
|
| 55 |
+
g_string = ET.tostring(g_element, encoding='unicode')
|
| 56 |
+
|
| 57 |
+
svg_document = f'''<?xml version="1.0" encoding="UTF-8"?>
|
| 58 |
+
<svg width="{size}px" height="{size}px" viewBox="0 0 109 109" version="1.1" xmlns="http://www.w3.org/2000/svg">
|
| 59 |
+
<g fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round">
|
| 60 |
+
{g_string}
|
| 61 |
+
</g>
|
| 62 |
+
</svg>'''
|
| 63 |
+
|
| 64 |
+
output_filename = output_path / f"U+{hex_codepoint}.png"
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
# --- MODIFICATION HERE ---
|
| 68 |
+
# Added the 'background_color' parameter to the rendering function.
|
| 69 |
+
cairosvg.svg2png(
|
| 70 |
+
bytestring=svg_document.encode('utf-8'),
|
| 71 |
+
write_to=str(output_filename),
|
| 72 |
+
background_color=bg_color
|
| 73 |
+
)
|
| 74 |
+
if (i + 1) % 100 == 0 or i == total_kanji - 1:
|
| 75 |
+
print(f"[{i+1}/{total_kanji}] Successfully rendered: {output_filename.name}")
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"[{i+1}/{total_kanji}] Failed to render {kanji_id}: {e}")
|
| 78 |
+
|
| 79 |
+
print("\nBatch rendering complete.")
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
image_directory = "kanji_images"
|
| 84 |
+
kanjivg_xml_path = "kanjivg-20220427.xml.xml"
|
| 85 |
+
image_size = 1024
|
| 86 |
+
bg_color = "white"
|
| 87 |
+
|
| 88 |
+
batch_render_kanji(kanjivg_xml_path, image_directory, image_size, bg_color)
|