-
Notifications
You must be signed in to change notification settings - Fork 43
/
batch.py
76 lines (58 loc) · 1.73 KB
/
batch.py
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
import json
import os
import shutil
import cv2
import glob
TEMP_DIR = 'images_out/temp'
BASE_SETTINGS = {
"width": 1024,
"height": 1536,
"ViTL14": False,
"steps": 150,
"skip_steps_ratio": 0.5,
}
GENERATION_SETTINGS = {
**BASE_SETTINGS,
"use_secondary_model": False,
"width": int(BASE_SETTINGS["width"] / 2),
"height": int(BASE_SETTINGS["height"] / 2),
"init_image": "",
"skip_steps": 0,
"clamp_max": 0.02,
}
FINISHING_SETTINGS = {
**BASE_SETTINGS,
"use_secondary_model": True,
"init_image": TEMP_DIR + "/upscaled.png",
"skip_steps": int(BASE_SETTINGS["steps"] * BASE_SETTINGS["skip_steps_ratio"]),
}
def apply_settings(new_settings):
with open('settings.json', 'r') as file:
current_settings = json.load(file)
with open('settings_temp.json', 'w') as file:
file.write(json.dumps({
**current_settings,
**new_settings,
}))
def upscale(filepath):
if not os.path.exists(TEMP_DIR):
os.mkdir(TEMP_DIR)
img = cv2.imread(filepath, 1)
scaled_img = cv2.resize(img, (0, 0), fx=2, fy=2)
cv2.imwrite(TEMP_DIR + '/upscaled.png', scaled_img)
def generate_image():
apply_settings(GENERATION_SETTINGS)
os.system("python prd.py -s settings_temp.json -o temp")
def finish_image(filepath=None):
if not filepath:
filepath = sorted(glob.glob(TEMP_DIR + '/temp*.png'), key=os.path.getctime)[-1]
upscale(filepath)
apply_settings(FINISHING_SETTINGS)
os.system("python prd.py -s settings_temp.json")
def finish_all():
for filepath in glob.glob(TEMP_DIR + '/temp*.png'):
finish_image(filepath)
if __name__ == '__main__':
for _ in range(10):
generate_image()
# finish_image()