Upload image_size_randomize.py
Browse files- image_size_randomize.py +55 -0
image_size_randomize.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import modules.scripts as scripts
|
3 |
+
import gradio as gr
|
4 |
+
import os
|
5 |
+
|
6 |
+
from modules import images
|
7 |
+
from modules.processing import process_images, Processed
|
8 |
+
from modules.shared import opts, cmd_opts, state
|
9 |
+
|
10 |
+
|
11 |
+
class RandomImageSizeScript(scripts.Script):
|
12 |
+
def title(self):
|
13 |
+
return "Image Size Randomize"
|
14 |
+
|
15 |
+
def ui(self, is_txt2img):
|
16 |
+
with gr.Tabs():
|
17 |
+
# Tab for direct text input
|
18 |
+
with gr.TabItem("Image Sizes"):
|
19 |
+
gr.HTML("<p>Enter image sizes in the format 'width,height', separated by new lines.</p>")
|
20 |
+
with gr.Row():
|
21 |
+
text_sizes = gr.Textbox(
|
22 |
+
label='Image Sizes',
|
23 |
+
placeholder='Enter sizes, e.g.:\n1024,512\n1024,768\n1024,1024',
|
24 |
+
elem_id=self.elem_id("text_sizes"),
|
25 |
+
lines=10 # Adjust line count for better usability
|
26 |
+
)
|
27 |
+
with gr.Row():
|
28 |
+
random_swap_text = gr.Checkbox(
|
29 |
+
label='Randomly swap width and height',
|
30 |
+
elem_id=self.elem_id("random_swap_text")
|
31 |
+
)
|
32 |
+
|
33 |
+
return [text_sizes, random_swap_text]
|
34 |
+
|
35 |
+
def run(self, p, text_sizes, random_swap_text):
|
36 |
+
sizes = []
|
37 |
+
|
38 |
+
# Read sizes from the textbox
|
39 |
+
if text_sizes:
|
40 |
+
try:
|
41 |
+
sizes = [tuple(map(int, line.strip().split(','))) for line in text_sizes.strip().split('\n') if line.strip()]
|
42 |
+
except ValueError:
|
43 |
+
print(f"Invalid size format in textbox: {text_sizes}")
|
44 |
+
|
45 |
+
if sizes:
|
46 |
+
width, height = random.choice(sizes)
|
47 |
+
if random_swap_text:
|
48 |
+
if random.choice([True, False]):
|
49 |
+
width, height = height, width
|
50 |
+
p.width, p.height = width, height
|
51 |
+
else:
|
52 |
+
print(f"No valid sizes found. Please check the textbox input.")
|
53 |
+
|
54 |
+
proc = process_images(p)
|
55 |
+
return proc
|