-
Notifications
You must be signed in to change notification settings - Fork 0
/
Random-Resolution.py
139 lines (117 loc) · 4.93 KB
/
Random-Resolution.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import gradio as gr
import random
import time
import modules.scripts as scripts
from modules import processing
from modules.processing import Processed
from modules.shared import state
class Script(scripts.Script):
def __init__(self):
# Defining Default Resolution Values as Class Properties
self.default_resolutions = [
(1024, 1024),
(1344, 768),
(1216, 832),
(1152, 896),
(896, 1152),
(832, 1216),
(768, 1334),
(0, 0),
(0, 0)
]
def title(self):
return "Random-Resolution"
def show(self, is_img2img):
return True
def ui(self, is_img2img):
with gr.Group():
with gr.Row():
enable_random = gr.Checkbox(label="Enable Random Resolution", value=False)
# UI components for nine resolution settings
resolution_rows = []
for i in range(9):
with gr.Row():
with gr.Column(scale=1):
use_resolution = gr.Checkbox(label=f"#{i+1}", value=True if i < 7 else False)
with gr.Column(scale=4):
with gr.Row():
width = gr.Number(
label="Width",
value=self.default_resolutions[i][0],
precision=0,
step=8
)
gr.HTML("×", elem_classes="resolution-separator")
height = gr.Number(
label="Height",
value=self.default_resolutions[i][1],
precision=0,
step=8
)
resolution_rows.append((use_resolution, width, height))
with gr.Row():
batch_count = gr.Slider(minimum=1, maximum=999, step=1, label="Number of Batches", value=1)
delay_time = gr.Number(label="Delay between batches (seconds)", value=0, precision=1)
# Add CSS Style
gr.HTML("""
<style>
.resolution-separator {
padding: 0 5px;
display: flex;
align-items: center;
justify-content: center;
}
/* Number Adjust the width of the input field */
input[type=number] {
width: 6em !important;
}
</style>
""")
# Consolidate UI components into one list
return [enable_random, batch_count, delay_time] + [item for row in resolution_rows for item in row]
def run(self, p, enable_random, batch_count, delay_time, *resolution_args):
# Reconfigure resolution_args for ease of use
resolutions = []
for i in range(0, len(resolution_args), 3):
use_res = resolution_args[i]
width = int(resolution_args[i + 1]) if resolution_args[i + 1] is not None else 0
height = int(resolution_args[i + 2]) if resolution_args[i + 2] is not None else 0
if use_res and width > 0 and height > 0:
resolutions.append((width, height))
if not resolutions:
print("No valid resolutions selected. Using default resolution.")
return processing.process_images(p)
batch_count = int(batch_count)
state.job_count = batch_count
images_list = []
all_prompts = []
infotexts = []
# Processing by each batch
for i in range(batch_count):
state.job = f"{i+1} out of {batch_count}"
if state.interrupted:
break
# Random selection only when random resolution selection is enabled
if enable_random and resolutions:
selected_width, selected_height = random.choice(resolutions)
p.width = selected_width
p.height = selected_height
# Image creation processing
proc = processing.process_images(p)
# Save results
images_list.extend(proc.images)
all_prompts.extend(proc.all_prompts)
infotexts.extend(proc.infotexts)
# Apply delay only if it is not the last batch
if i < batch_count - 1:
time.sleep(delay_time)
# Update seed value (for next batch)
p.seed = random.randint(0, 2**32 - 1)
return Processed(
p,
images_list,
p.seed,
"",
all_prompts=all_prompts,
infotexts=infotexts
)