Spaces:
Sleeping
Sleeping
Upload whiteout2.py
Browse files- whiteout2.py +68 -0
whiteout2.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
from PIL import Image, ImageDraw
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
MAX_SIDE = 900
|
| 6 |
+
|
| 7 |
+
def fit_canvas(img):
|
| 8 |
+
if img is None:
|
| 9 |
+
return MAX_SIDE, MAX_SIDE
|
| 10 |
+
w, h = img.size
|
| 11 |
+
scale = MAX_SIDE / max(w, h)
|
| 12 |
+
return int(w * scale), int(h * scale)
|
| 13 |
+
|
| 14 |
+
def snap_last_stroke(val):
|
| 15 |
+
"""Convert any roughly-dragged stroke into a filled rectangle of its color."""
|
| 16 |
+
if not val or not val["layers"]:
|
| 17 |
+
return val
|
| 18 |
+
last = val["layers"][-1]
|
| 19 |
+
box = last.getbbox()
|
| 20 |
+
if box:
|
| 21 |
+
mx, my = (box[0]+box[2])//2, (box[1]+box[3])//2
|
| 22 |
+
try:
|
| 23 |
+
color = last.getpixel((mx, my))
|
| 24 |
+
except:
|
| 25 |
+
color = (255,255,255,255)
|
| 26 |
+
rect = Image.new("RGBA", last.size, (0,0,0,0))
|
| 27 |
+
ImageDraw.Draw(rect).rectangle(box, fill=color)
|
| 28 |
+
val["layers"][-1] = rect
|
| 29 |
+
base = val["background"].copy()
|
| 30 |
+
for layer in val["layers"]:
|
| 31 |
+
base.alpha_composite(layer)
|
| 32 |
+
val["composite"] = base
|
| 33 |
+
# collapse layers so editor.value is always the merged image
|
| 34 |
+
val["background"], val["layers"] = base, []
|
| 35 |
+
return val
|
| 36 |
+
|
| 37 |
+
def load_image(img):
|
| 38 |
+
return gr.update(value=img, canvas_size=fit_canvas(img))
|
| 39 |
+
|
| 40 |
+
def update_brush(color_hex):
|
| 41 |
+
return gr.update(
|
| 42 |
+
brush=gr.Brush(
|
| 43 |
+
colors=[color_hex],
|
| 44 |
+
default_color=color_hex,
|
| 45 |
+
default_size=80,
|
| 46 |
+
color_mode="fixed"
|
| 47 |
+
)
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
with gr.Blocks(css=".gradio-container { max-width:1200px; margin:auto; }") as demo:
|
| 51 |
+
with gr.Row():
|
| 52 |
+
color_picker = gr.ColorPicker("#FFFFFF", label="Brush Color")
|
| 53 |
+
uploader = gr.Image(type="pil", label="Upload Image")
|
| 54 |
+
editor = gr.ImageEditor(
|
| 55 |
+
type="pil", # emit PIL.Images so the download icon works
|
| 56 |
+
brush=gr.Brush(colors=["#FFFFFF"], default_color="#FFFFFF", default_size=80, color_mode="fixed"),
|
| 57 |
+
eraser=gr.Eraser(default_size=80),
|
| 58 |
+
layers=False,
|
| 59 |
+
canvas_size=(MAX_SIDE, MAX_SIDE),
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# snapping callback
|
| 63 |
+
editor.apply(snap_last_stroke, inputs=editor, outputs=editor)
|
| 64 |
+
# wire color picker & uploader
|
| 65 |
+
color_picker.change(update_brush, color_picker, editor)
|
| 66 |
+
uploader.change(load_image, uploader, editor)
|
| 67 |
+
|
| 68 |
+
demo.launch(share=True)
|