#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 日本語文字画像 + 英語説明文(CC0) - 単語は外部ファイルから読み込み(UTF-8, 1行1語) - 白背景・黒文字 - 説明文に色指定 - PNG+TXTペアを train/ に出力 - 最後に train.tar.gz にまとめる """ import csv, random, argparse, hashlib, datetime, tarfile, shutil from pathlib import Path from dataclasses import dataclass from PIL import Image, ImageDraw, ImageFont import numpy as np # -------------------- # 定数 # -------------------- DEFAULT_FONTS_DIR = Path("./fonts") TRAIN_DIR = Path("./train") LINE_SPACING = 1.25 @dataclass class Example: jp_text: str en_desc: str # -------------------- # 外部ファイルから単語読み込み # -------------------- def load_words(file_path: Path): if not file_path.exists(): raise FileNotFoundError(f"単語ファイルが見つかりません: {file_path}") with open(file_path, "r", encoding="utf-8") as f: words = [line.strip() for line in f if line.strip()] if not words: raise ValueError(f"単語ファイルが空です: {file_path}") return words # -------------------- # データ生成 # -------------------- def gen_examples(n:int, seed:int, words:list[str]): random.seed(seed) exs = [] for _ in range(n): jp = random.choice(words) desc = f'This image is saying "{jp}". The background is white. The letter is black.' exs.append(Example(jp_text=jp, en_desc=desc)) return exs # -------------------- # フォント # -------------------- def list_fonts(font_dir:Path): fonts = [p for p in font_dir.glob("*") if p.suffix.lower() in (".ttf",".otf",".ttc",".otc")] if not fonts: raise FileNotFoundError(f"フォントが見つかりません: {font_dir} にOFL/PDの日本語フォントを置いてください") return fonts # -------------------- # 描画(背景:白、文字:黒) # -------------------- def draw_horizontal(text, font_path:Path, size, max_font_size, min_font_size, margin_px): W,H = size img = Image.new("RGB", (W,H), (255,255,255)) # 白背景 draw = ImageDraw.Draw(img) for fs in range(max_font_size, min_font_size-1, -2): font = ImageFont.truetype(str(font_path), fs) bbox = draw.textbbox((0,0), text, font=font) w, h = bbox[2] - bbox[0], bbox[3] - bbox[1] if w <= W - 2*margin_px and h <= H - 2*margin_px: break x = (W - w)//2 y = (H - h)//2 draw.text((x, y), text, font=font, fill=(0,0,0)) # 黒 return img def draw_vertical(text, font_path:Path, size, max_font_size, min_font_size, margin_px): W,H = size img = Image.new("RGB", (W,H), (255,255,255)) # 白背景 draw = ImageDraw.Draw(img) for fs in range(max_font_size, min_font_size-1, -2): font = ImageFont.truetype(str(font_path), fs) line_h = font.getbbox("Hg")[3] - font.getbbox("Hg")[1] step = int(line_h * LINE_SPACING) total_h = len(text) * step if text else step if text: widths = [] for c in text: cb = draw.textbbox((0,0), c, font=font) widths.append(cb[2] - cb[0]) col_w = max(widths) else: cb = draw.textbbox((0,0), "あ", font=font) col_w = cb[2] - cb[0] if col_w <= W - 2*margin_px and total_h <= H - 2*margin_px: break x = (W - col_w)//2 y = (H - total_h)//2 for i, ch in enumerate(text): draw.text((x, y + i*step), ch, font=font, fill=(0,0,0)) # 黒 return img # -------------------- # その他 # -------------------- def sha1_of_text(t:str)->str: import hashlib return hashlib.sha1(t.encode("utf-8")).hexdigest()[:16] def write_license_file(): text = f"""Dataset License (CC0, JP glyphs + EN descriptions) Copyright (c) {datetime.date.today().year} YOUR_NAME All images (Japanese text) and English descriptions are synthetic and authored by the dataset creator. Released under CC0-1.0 (Public Domain Dedication). """ Path("./LICENSE.txt").write_text(text, encoding="utf-8") def append_assets_registry(font_paths): reg_path = Path("./provenance/assets_registry.csv") reg_path.parent.mkdir(parents=True, exist_ok=True) new = not reg_path.exists() with open(reg_path, "a", newline="", encoding="utf-8") as f: w = csv.writer(f) if new: w.writerow(["asset_type","path","license","notes"]) for p in font_paths: w.writerow(["font", str(p), "SIL Open Font License (assumed)", "同梱時は各フォントのLICENSEを添付"]) def make_tarfile(source_dir: Path, output_filename: Path, remove_source=False): with tarfile.open(output_filename, "w:gz") as tar: tar.add(source_dir, arcname=source_dir.name) if remove_source: shutil.rmtree(source_dir) # -------------------- # メイン # -------------------- def main(n_train, seed, mode, img_size, max_font_size, min_font_size, margin_px, words_file, archive, remove_source): random.seed(seed); np.random.seed(seed) TRAIN_DIR.mkdir(parents=True, exist_ok=True) write_license_file() fonts = list_fonts(DEFAULT_FONTS_DIR) append_assets_registry(fonts) words = load_words(words_file) exs = gen_examples(n_train, seed, words) def render(jp_text, font_path, writing_mode): if writing_mode == "horizontal": return draw_horizontal(jp_text, font_path, img_size, max_font_size, min_font_size, margin_px) elif writing_mode == "vertical": return draw_vertical(jp_text, font_path, img_size, max_font_size, min_font_size, margin_px) for i, ex in enumerate(exs): font = random.choice(fonts) writing_mode = random.choice(["horizontal","vertical"]) if mode=="both" else mode img = render(ex.jp_text, font, writing_mode) uid = sha1_of_text(f"{i}-{ex.jp_text}-{font.name}-{writing_mode}-{img_size}-{max_font_size}-{min_font_size}") img_path = TRAIN_DIR/f"{uid}.png" txt_path = TRAIN_DIR/f"{uid}.txt" img.save(img_path) txt_path.write_text(ex.en_desc, encoding="utf-8") print(f"Generated {n_train} samples in {TRAIN_DIR}") if archive: tar_path = Path("./train.tar.gz") make_tarfile(TRAIN_DIR, tar_path, remove_source=remove_source) print(f"Created archive: {tar_path}") if __name__ == "__main__": ap = argparse.ArgumentParser() ap.add_argument("--n_train", type=int, default=1000) ap.add_argument("--seed", type=int, default=0) ap.add_argument("--mode", type=str, default="both", choices=["horizontal","vertical","both"]) ap.add_argument("--img_size", type=int, nargs=2, metavar=("WIDTH","HEIGHT"), default=(640,640)) ap.add_argument("--max_font_size", type=int, default=54) ap.add_argument("--min_font_size", type=int, default=28) ap.add_argument("--margin_px", type=int, default=28) ap.add_argument("--words_file", type=Path, required=True, help="日本語単語リストファイル(UTF-8, 1行1語)") ap.add_argument("--archive", action="store_true", help="train/ を tar.gz にまとめる") ap.add_argument("--remove_source", action="store_true", help="tar作成後に train/ を削除") args = ap.parse_args() main(args.n_train, args.seed, args.mode, tuple(args.img_size), args.max_font_size, args.min_font_size, args.margin_px, args.words_file, args.archive, args.remove_source)