Skip to content

Commit f8a8c99

Browse files
committed
fix #3248
1 parent 83d3f6c commit f8a8c99

File tree

8 files changed

+93
-100
lines changed

8 files changed

+93
-100
lines changed

demo/blocks_flipper/image.png

-842 Bytes
Binary file not shown.

demo/blocks_flipper/run.py

+21-40
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,32 @@
1-
# import numpy as np
2-
# import gradio as gr
3-
4-
# def flip_text(x):
5-
# return x[::-1]
6-
7-
# def flip_image(x):
8-
# return np.fliplr(x)
9-
10-
# with gr.Blocks() as demo:
11-
# gr.Markdown("Flip text or image files using this demo.")
12-
# with gr.Tab("Flip Text"):
13-
# text_input = gr.Textbox()
14-
# text_output = gr.Textbox()
15-
# text_button = gr.Button("Flip")
16-
# with gr.Tab("Flip Image"):
17-
# with gr.Row():
18-
# image_input = gr.Image()
19-
# image_output = gr.Image()
20-
# image_button = gr.Button("Flip")
21-
22-
# with gr.Accordion("Open for More!"):
23-
# gr.Markdown("Look at me...")
24-
25-
# text_button.click(flip_text, inputs=text_input, outputs=text_output)
26-
# image_button.click(flip_image, inputs=image_input, outputs=image_output)
27-
28-
# if __name__ == "__main__":
29-
# demo.launch()
30-
1+
import numpy as np
312
import gradio as gr
32-
from PIL import Image
333

34-
img = Image.new("RGB", (512, 512), (0, 0, 0))
35-
img.save("image.png", "PNG")
364

37-
print(img)
5+
def flip_text(x):
6+
return x[::-1]
387

398

40-
def save_image(image):
41-
image.save("colorede.png")
42-
return image
9+
def flip_image(x):
10+
return np.fliplr(x)
4311

4412

4513
with gr.Blocks() as demo:
46-
image = gr.Image(value="image.png", interactive=True, tool="color-sketch")
47-
button = gr.Button()
48-
button.click(save_image, inputs=image, outputs=[])
14+
gr.Markdown("Flip text or image files using this demo.")
15+
with gr.Tab("Flip Text"):
16+
text_input = gr.Textbox()
17+
text_output = gr.Textbox()
18+
text_button = gr.Button("Flip")
19+
with gr.Tab("Flip Image"):
20+
with gr.Row():
21+
image_input = gr.Image()
22+
image_output = gr.Image()
23+
image_button = gr.Button("Flip")
24+
25+
with gr.Accordion("Open for More!"):
26+
gr.Markdown("Look at me...")
27+
28+
text_button.click(flip_text, inputs=text_input, outputs=text_output)
29+
image_button.click(flip_image, inputs=image_input, outputs=image_output)
4930

5031
if __name__ == "__main__":
5132
demo.launch()

demo/blocks_mask/colorede.png

12.8 KB
Loading

demo/blocks_mask/image.png

1.83 KB
Loading

demo/blocks_mask/run.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import gradio as gr
22
from gradio.components import Markdown as md
3+
from PIL import Image
34

45
demo = gr.Blocks()
56

@@ -53,6 +54,21 @@
5354
)
5455

5556

57+
def save_image(image):
58+
image.save("colorede.png")
59+
return image
60+
61+
62+
img = Image.new("RGB", (512, 512), (150, 150, 150))
63+
img.save("image.png", "PNG")
64+
65+
io5d = gr.Interface(
66+
save_image,
67+
gr.Image("image.png", source="upload", tool="color-sketch", type="pil"),
68+
gr.Image(),
69+
)
70+
71+
5672
with demo:
5773
md("# Different Ways to Use the Image Input Component")
5874
md(
@@ -130,6 +146,8 @@
130146
io3b2.render()
131147
with gr.Tab("Two"):
132148
io3b3.render()
149+
md("**5d. Color Sketchpad with image upload and a default images**")
150+
io5d.render()
133151

134152

135153
if __name__ == "__main__":

ui/packages/image/src/Image.svelte

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
// @ts-nocheck
3-
import { createEventDispatcher, tick } from "svelte";
3+
import { createEventDispatcher, tick, onMount } from "svelte";
44
import { BlockLabel } from "@gradio/atoms";
55
import { Image, Sketch as SketchIcon } from "@gradio/icons";
66
@@ -98,7 +98,7 @@
9898
$: dispatch("drag", dragging);
9999
100100
function handle_image_load(event: Event) {
101-
const element = event.composedPath()[0] as HTMLImageElement;
101+
const element = event.currentTarget as HTMLImageElement;
102102
img_width = element.naturalWidth;
103103
img_height = element.naturalHeight;
104104
container_height = element.getBoundingClientRect().height;
@@ -158,6 +158,14 @@
158158
}
159159
}
160160
}
161+
162+
onMount(async () => {
163+
if (tool === "color-sketch" && value && typeof value === "string") {
164+
static_image = value;
165+
await tick();
166+
handle_image_load({ currentTarget: value_img });
167+
}
168+
});
161169
</script>
162170

163171
<BlockLabel
@@ -242,7 +250,7 @@
242250
{:else}
243251
<img
244252
src={value.image || value}
245-
alt=""
253+
alt="hello"
246254
class:webcam={source === "webcam" && mirror_webcam}
247255
/>
248256
{/if}

ui/packages/image/src/Sketch.svelte

-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
export let brush_color = "#0b0f19";
1515
export let brush_radius = 50;
1616
export let source;
17-
$: console.log($$props.width, old_height);
1817
1918
export let width = 400;
2019
export let height = 200;
@@ -33,7 +32,6 @@
3332
3433
$: {
3534
if (mounted && value_img !== last_value_img) {
36-
console.log("boo");
3735
last_value_img = value_img;
3836
3937
clear();
@@ -152,7 +150,6 @@
152150
153151
canvas_observer = new ResizeObserver((entries, observer, ...rest) => {
154152
handle_canvas_resize(entries, observer);
155-
console.log("RESIZE OBSERVER");
156153
});
157154
canvas_observer.observe(canvas_container);
158155
@@ -284,7 +281,6 @@
284281
let old_container_height = 0;
285282
286283
let handle_canvas_resize = async () => {
287-
console.log("RESIZE");
288284
if (
289285
width === old_width &&
290286
height === old_height &&
@@ -293,7 +289,6 @@
293289
return;
294290
}
295291
const dimensions = { width: width, height: height };
296-
console.log({ dimensions });
297292
298293
const container_dimensions = {
299294
height: container_height,
@@ -331,7 +326,6 @@
331326
332327
$: {
333328
if (width || height) {
334-
console.log("WIDTH?HEIGHT RESIZE");
335329
handle_canvas_resize();
336330
}
337331
}
@@ -341,7 +335,6 @@
341335
await tick();
342336
343337
const dpr = window.devicePixelRatio || 1;
344-
console.log(dpr, dimensions);
345338
canvas.width = dimensions.width * (scale ? dpr : 1);
346339
canvas.height = dimensions.height * (scale ? dpr : 1);
347340

0 commit comments

Comments
 (0)