Spaces:
Running
Running
File size: 1,673 Bytes
c10f4a4 62e1b92 c10f4a4 239d8d0 62e1b92 239d8d0 c10f4a4 239d8d0 47ca6a2 239d8d0 c10f4a4 239d8d0 |
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 |
import gradio as gr
import imageio
import numpy as np
def hex_to_rgb(hex_color):
"""Convert hex color string (#RRGGBB) to an (R, G, B) tuple."""
hex_color = hex_color.lstrip("#")
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
def create_blank_image(width, height, hex_color):
"""Create a new RGB image with given width, height, and color."""
r, g, b = hex_to_rgb(hex_color)
img = np.full((height, width, 3), (r, g, b), dtype=np.uint8)
return img
def dummy(img, invert_mask):
if img is None:
raise ValueError("No image provided.")
mask = img["mask"]
if invert_mask and mask is not None:
mask = 255 - np.array(mask)
imageio.imwrite("output_image.png", mask)
return img["image"], mask
with gr.Blocks() as demo:
with gr.Accordion("Create Blank Image", open=False):
with gr.Row():
width_input = gr.Number(label="Width", value=512, precision=0)
height_input = gr.Number(label="Height", value=512, precision=0)
color_input = gr.ColorPicker(label="Color", value="#969696") # hex color
create_btn = gr.Button("Create Image")
with gr.Row():
img = gr.Image(tool="sketch", label="base image", show_label=True, min_width=600)
img1 = gr.Image()
img2 = gr.Image(label="mask image", show_label=True)
invert = gr.Checkbox(label="Invert mask")
btn = gr.Button("Process")
btn.click(dummy, [img, invert], [img1, img2])
create_btn.click(create_blank_image,
[width_input, height_input, color_input],
img)
demo.launch(debug=True, show_error=True, quiet=False) |