release: lineartization v1.6.1 - color/handwritten image to line-art (skeleton & minimum modes, tunable denoise)
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
"""
|
||||
lineart-extractor 使用示例
|
||||
==========================
|
||||
演示三种用法: 一行函数 / 自定义配置 / 直接处理 ndarray
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from lineartization import (
|
||||
LineArtConfig,
|
||||
extract_lineart,
|
||||
extract_lineart_file,
|
||||
load_image,
|
||||
save_image,
|
||||
)
|
||||
|
||||
DEMO_SRC = os.environ.get("LINEART_DEMO_SRC", "手抄报.jpg")
|
||||
DEMO_OUT_DIR = os.environ.get("LINEART_DEMO_OUT", "./output")
|
||||
os.makedirs(DEMO_OUT_DIR, exist_ok=True)
|
||||
|
||||
|
||||
def demo_simple():
|
||||
"""① 一行搞定"""
|
||||
print("=== 示例1: 一行调用 ===")
|
||||
extract_lineart_file(DEMO_SRC, os.path.join(DEMO_OUT_DIR, "simple.png"))
|
||||
print(" 已生成 simple.png")
|
||||
|
||||
|
||||
def demo_config():
|
||||
"""② 自定义配置 (线宽 + 保护华表区)"""
|
||||
print("=== 示例2: 自定义配置 ===")
|
||||
cfg = LineArtConfig(
|
||||
method="minimum",
|
||||
denoise="strong",
|
||||
line_width=2, # 统一线宽 2px
|
||||
enable_green_smoothing=True, # 手抄报绿块抹平
|
||||
protect_areas=[(120, 220, 940, 1050)], # 保护"华表"区域
|
||||
)
|
||||
extract_lineart_file(DEMO_SRC, os.path.join(DEMO_OUT_DIR, "configured.png"),
|
||||
cfg, verbose=True)
|
||||
print(" 已生成 configured.png")
|
||||
|
||||
|
||||
def demo_ndarray():
|
||||
"""③ 直接处理 ndarray (可嵌入你自己的流水线)"""
|
||||
print("=== 示例3: ndarray 处理 ===")
|
||||
img = load_image(DEMO_SRC)
|
||||
print(f" 输入尺寸: {img.shape[1]}x{img.shape[0]}")
|
||||
lineart = extract_lineart(img, LineArtConfig(line_width=2))
|
||||
black_ratio = (lineart < 128).mean() * 100
|
||||
print(f" 黑占比: {black_ratio:.2f}%")
|
||||
save_image(os.path.join(DEMO_OUT_DIR, "ndarray.png"), lineart)
|
||||
print(" 已生成 ndarray.png")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if not os.path.exists(DEMO_SRC):
|
||||
print(f"提示: 未找到示例图片 '{DEMO_SRC}'")
|
||||
print("请设置环境变量 LINEART_DEMO_SRC 指向一张图片, 例如:")
|
||||
print(" set LINEART_DEMO_SRC=D:\\pics\\手抄报.jpg")
|
||||
sys.exit(0)
|
||||
|
||||
demo_simple()
|
||||
demo_config()
|
||||
demo_ndarray()
|
||||
print("\n全部示例完成 ✔")
|
||||
Reference in New Issue
Block a user