File size: 2,555 Bytes
96d2bbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
python convert_source_to_scribble_xdog,py "input_folder" "output_folder"
画像をスケッチ(線画)に変換する機能を提供します。
"""
import argparse
import os
from lineart_util import scribble_xdog
from PIL import Image
import numpy as np
from tqdm import tqdm
import cv2

def convert(image_path):
    """
    画像をスケッチに変換します。
    
    Args:
        image_path (str): 入力画像のパス
        
    Returns:
        str: 変換後の画像のパス
    """
    image = Image.open(image_path)
    return convert_pil_to_sketch(image)

def convert_pil_to_sketch(image):
    """
    PIL.Imageをスケッチに変換します。
    
    Args:
        image (PIL.Image): 入力画像
        
    Returns:
        PIL.Image: 変換後の画像
    """
    input_width, input_height = image.size
    image = np.array(image)
    processed_image, _ = scribble_xdog(image, 2048, 16)  # PIL.Image
    processed_image = processed_image.resize((input_width, input_height))
    # make PIL.Image to cv2 and INVERSE
    processed_image = cv2.cvtColor(np.array(processed_image), cv2.COLOR_RGB2BGR)
    processed_image = 255 - processed_image
    return Image.fromarray(processed_image)

def process_images(input_folder, output_folder):
    """
    フォルダ内の画像を一括変換します。
    
    Args:
        input_folder (str): 入力フォルダのパス
        output_folder (str): 出力フォルダのパス
    """
    # 入力フォルダ内の全ての画像ファイルを取得
    image_files = [f for f in os.listdir(input_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]

    # 出力フォルダを作成(存在しない場合)
    os.makedirs(output_folder, exist_ok=True)

    # 各画像ファイルを処理
    for image_file in tqdm(image_files):
        input_path = os.path.join(input_folder, image_file)
        output_path = os.path.join(output_folder, image_file)

        processed_image = convert_pil_to_sketch(Image.open(input_path))
        processed_image.save(output_path)

if __name__ == '__main__':
    import argparse
    
    parser = argparse.ArgumentParser(description='指定したフォルダ内の全ての画像をscribble_xdogで処理し、出力フォルダに保存します。')
    parser.add_argument('input_folder', type=str, help='入力フォルダのパス')
    parser.add_argument('output_folder', type=str, help='出力フォルダのパス')

    args = parser.parse_args()
    process_images(args.input_folder, args.output_folder)