File size: 2,578 Bytes
06a5d67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6037ba
 
 
 
 
 
06a5d67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#### huggingface-cli download svjack/Genshin-Impact-Portrait-with-Tags-Filtered-IID-Gender-SP --include "genshin_impact_ALBEDO_images_and_texts/*" --local-dir .

#### mv genshin_impact_ALBEDO_oil_painting_and_texts genshin_impact_ALBEDO_hidiffusion_images_and_texts

import os
from tqdm import tqdm
from gradio_client import Client, handle_file
from PIL import Image
from shutil import copy2

# 初始化客户端
client = Client("http://localhost:7860")

# 输入和输出文件夹路径
input_dir = "genshin_impact_ALBEDO_images_and_texts"
output_dir = "genshin_impact_ALBEDO_oil_painting_and_texts"

# 创建输出文件夹(如果不存在)
os.makedirs(output_dir, exist_ok=True)

# 获取所有.png文件
png_files = [f for f in os.listdir(input_dir) if f.endswith('.png')]

# 遍历每个.png文件
for png_file in tqdm(png_files, desc="Processing images"):
    # 获取对应的.txt文件名
    base_name = os.path.splitext(png_file)[0]
    txt_file = base_name + ".txt"
    
    # 检查.txt文件是否存在
    txt_path = os.path.join(input_dir, txt_file)
    if not os.path.exists(txt_path):
        print(f"Warning: No corresponding .txt file found for {png_file}")
        continue
    
    # 读取.txt文件内容并修改prompt
    with open(txt_path, 'r', encoding='utf-8') as f:
        prompt = f.read()
        ####.replace(
        ####    "In this digital anime-style drawing,",
        ####    "In this realistic personal drawing,"
        ####)
    prompt = ",".join(["In this realistic personal drawing"] + prompt.split(",")[1:])
    
    # 调用API
    result = client.predict(
        input_image=handle_file(os.path.join(input_dir, png_file)),
        prompt=prompt,
        negative_prompt="blurry, ugly, duplicate, poorly drawn, deformed, mosaic",
        seed=2718545171199645000,
        guidance_scale=8.5,
        scale=2,
        controlnet_conditioning_scale=0.5,
        strength=1,
        controlnet_start=0,
        controlnet_end=1,
        guassian_sigma=2,
        intensity_threshold=3,
        api_name="/predict"
    )
    
    # 获取生成的图像路径
    generated_image_path = result[-1][1]
    
    # 使用PIL打开图像并保存到输出文件夹
    output_image_path = os.path.join(output_dir, png_file)
    with Image.open(generated_image_path) as img:
        img.save(output_image_path)
    
    # 保存修改后的prompt到.txt文件
    output_txt_path = os.path.join(output_dir, txt_file)
    with open(output_txt_path, 'w', encoding='utf-8') as f:
        f.write(prompt)

print("Processing completed!")