Skip to content

Commit ce95d0a

Browse files
authored
support custom output image size in img2img gradio (#2767)
1 parent 5203929 commit ce95d0a

File tree

2 files changed

+379
-16
lines changed

2 files changed

+379
-16
lines changed

notebooks/image-to-image-genai/gradio_helper.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
def make_demo(pipeline, generator_cls, image_to_tensor):
18-
def infer(input_image, prompt, negative_prompt, seed, strength, randomize_seed, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
18+
def infer(input_image, prompt, negative_prompt, seed, strength, randomize_seed, num_inference_steps, use_custom_size, height, width, progress=gr.Progress(track_tqdm=True)):
1919
if randomize_seed:
2020
seed = np.random.randint(0, MAX_SEED)
2121

@@ -25,9 +25,16 @@ def infer(input_image, prompt, negative_prompt, seed, strength, randomize_seed,
2525
pbar = tqdm(total=math.ceil((num_inference_steps + 1) * strength))
2626

2727
def callback(step, num_steps, latent):
28+
if pbar.total != num_steps:
29+
pbar.reset(total=num_steps)
2830
pbar.update(1)
2931
sys.stdout.flush()
3032
return False
33+
additional_args = {}
34+
35+
if use_custom_size:
36+
additional_args = {"height": height, "width": width}
37+
3138

3239
image_tensor = pipeline.generate(
3340
prompt,
@@ -37,6 +44,7 @@ def callback(step, num_steps, latent):
3744
generator=generator,
3845
strength=strength,
3946
callback=callback,
47+
**additional_args
4048
)
4149

4250
return image_tensor.data[0], seed
@@ -93,12 +101,14 @@ def callback(step, num_steps, latent):
93101
step=1,
94102
value=20,
95103
)
96-
104+
use_custom_size = gr.Checkbox(label="Use custom height and width", value=False)
105+
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=64, value=512)
106+
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=64, value=512)
97107
gr.Examples(examples=examples, inputs=[input_image, prompt])
98108
gr.on(
99109
triggers=[run_button.click, prompt.submit, negative_prompt.submit],
100110
fn=infer,
101-
inputs=[input_image, prompt, negative_prompt, seed, strength, randomize_seed, num_inference_steps],
111+
inputs=[input_image, prompt, negative_prompt, seed, strength, randomize_seed, num_inference_steps, use_custom_size, height, width],
102112
outputs=[result, seed],
103113
)
104114

0 commit comments

Comments
 (0)