Spaces:
Running
Running
| 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) |