oh-my-ocr / text_renderer

https://oh-my-ocr.github.io/text_renderer/README.html
MIT License
802 stars 161 forks source link

您好,请问如何遍历10种ttf文件,分别生成500张字体图片,一种字体生成500张 #42

Closed lxwkobe24 closed 2 years ago

Sanster commented 2 years ago

目前不支持指定单独的字体文件,可以准备十个 font_list,供参考代码:

import inspect
import os
from pathlib import Path

from text_renderer.effect import *
from text_renderer.corpus import *
from text_renderer.config import (
    RenderCfg,
    NormPerspectiveTransformCfg,
    GeneratorCfg,
    FixedTextColorCfg,
)

CURRENT_DIR = Path(os.path.abspath(os.path.dirname(__file__)))
OUT_DIR = CURRENT_DIR / "output"
DATA_DIR = CURRENT_DIR
BG_DIR = DATA_DIR / "bg"
CHAR_DIR = DATA_DIR / "char"
FONT_DIR = DATA_DIR / "font"
FONT_LIST_DIR = DATA_DIR / "font_list"
TEXT_DIR = DATA_DIR / "text"

perspective_transform = NormPerspectiveTransformCfg(20, 20, 1.5)

def get_char_corpus(one_font_list_file):
    return CharCorpus(
        CharCorpusCfg(
            text_paths=[TEXT_DIR / "chn_text.txt", TEXT_DIR / "eng_text.txt"],
            filter_by_chars=True,
            chars_file=CHAR_DIR / "chn.txt",
            length=(5, 10),
            char_spacing=(-0.3, 1.3),
            font_dir=FONT_DIR,
            font_list_file=FONT_LIST_DIR / one_font_list_file,
            font_size=(30, 31)
        )
    )

def base_cfg(
    name: str, corpus, corpus_effects=None, layout_effects=None, layout=None, gray=True
):
    return GeneratorCfg(
        num_image=50,
        save_dir=OUT_DIR / name,
        render_cfg=RenderCfg(
            bg_dir=BG_DIR,
            perspective_transform=perspective_transform,
            gray=gray,
            layout_effects=layout_effects,
            layout=layout,
            corpus=corpus,
            corpus_effects=corpus_effects,
        ),
    )

def many_font():
    out = []
    # 一个文件列表一种字体
    for one_font_list_file in ["font1.txt", "font2.txt"]:
        out.append(base_cfg(
            inspect.currentframe().f_code.co_name,
            corpus=get_char_corpus(one_font_list_file),
            corpus_effects=Effects(
                [
                    Line(0.5, color_cfg=FixedTextColorCfg()),
                    OneOf([DropoutRand(), DropoutVertical()]),
                ]
            ),
        ))
    return out

configs = [*many_font()]
lxwkobe24 commented 2 years ago

谢谢呀

lxwkobe24 commented 2 years ago

目前不支持指定单独的字体文件,可以准备十个 font_list,供参考代码:

import inspect
import os
from pathlib import Path

from text_renderer.effect import *
from text_renderer.corpus import *
from text_renderer.config import (
    RenderCfg,
    NormPerspectiveTransformCfg,
    GeneratorCfg,
    FixedTextColorCfg,
)

CURRENT_DIR = Path(os.path.abspath(os.path.dirname(__file__)))
OUT_DIR = CURRENT_DIR / "output"
DATA_DIR = CURRENT_DIR
BG_DIR = DATA_DIR / "bg"
CHAR_DIR = DATA_DIR / "char"
FONT_DIR = DATA_DIR / "font"
FONT_LIST_DIR = DATA_DIR / "font_list"
TEXT_DIR = DATA_DIR / "text"

perspective_transform = NormPerspectiveTransformCfg(20, 20, 1.5)

def get_char_corpus(one_font_list_file):
    return CharCorpus(
        CharCorpusCfg(
            text_paths=[TEXT_DIR / "chn_text.txt", TEXT_DIR / "eng_text.txt"],
            filter_by_chars=True,
            chars_file=CHAR_DIR / "chn.txt",
            length=(5, 10),
            char_spacing=(-0.3, 1.3),
            font_dir=FONT_DIR,
            font_list_file=FONT_LIST_DIR / one_font_list_file,
            font_size=(30, 31)
        )
    )

def base_cfg(
    name: str, corpus, corpus_effects=None, layout_effects=None, layout=None, gray=True
):
    return GeneratorCfg(
        num_image=50,
        save_dir=OUT_DIR / name,
        render_cfg=RenderCfg(
            bg_dir=BG_DIR,
            perspective_transform=perspective_transform,
            gray=gray,
            layout_effects=layout_effects,
            layout=layout,
            corpus=corpus,
            corpus_effects=corpus_effects,
        ),
    )

def many_font():
    out = []
    # 一个文件列表一种字体
    for one_font_list_file in ["font1.txt", "font2.txt"]:
        out.append(base_cfg(
            inspect.currentframe().f_code.co_name,
            corpus=get_char_corpus(one_font_list_file),
            corpus_effects=Effects(
                [
                    Line(0.5, color_cfg=FixedTextColorCfg()),
                    OneOf([DropoutRand(), DropoutVertical()]),
                ]
            ),
        ))
    return out

configs = [*many_font()]

您好,请问这个代码如何让它输出的时候,我是十种ttf文件,然后我想每一个ttf都有500张图片的输出,这个程序运行起来,输出那里会报错的,方便告知如何更改输出图片那里吗,谢谢