gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
30.77k stars 2.29k forks source link

Is there no component specialized for inpainting in Gradio 4.x? #8692

Open gibiee opened 4 days ago

gibiee commented 4 days ago

Is your feature request related to a problem? Please describe.

Describe the solution you'd like

Additional context

# python 3.10.14 / gradio 3.50.2

import gradio as gr
from PIL import Image
import numpy as np

def rgb_to_bgr(paint) :
    image, mask = paint['image'], paint['mask'].convert('L')
    image, mask = np.array(image), np.array(mask)

    mask_bool = np.where(mask > 128, True, False)
    image[mask_bool] = image[mask_bool, ::-1]
    return Image.fromarray(image)

with gr.Blocks() as demo:
    with gr.Row():
        input_img = gr.Image(label='Input', type='pil', image_mode='RGB', source='upload', tool='sketch')

        output_img = gr.Image(label='Output', type='pil', image_mode='RGB')

    with gr.Row() :
        btn = gr.Button("Run", variant="primary")

    btn.click(fn=rgb_to_bgr, inputs=[input_img], outputs=[output_img])

demo.launch()
# python 3.10.14 / gradio 4.37.2

import gradio as gr
from PIL import Image
import numpy as np

def rgb_to_bgr(paint) :
    image, mask = paint['background'], paint['layers'][0].convert('L')
    image, mask = np.array(image), np.array(mask)

    mask_bool = np.where(mask > 128, True, False)
    image[mask_bool] = image[mask_bool, ::-1]
    return Image.fromarray(image)

with gr.Blocks() as demo:
    with gr.Row():
        input_img = gr.ImageEditor(label='Input', type='pil', image_mode='RGB', sources=['upload'], layers=False,
                                   brush=gr.Brush(colors=["#AAAAAA"], color_mode="fixed"))

        output_img = gr.Image(label='Output', type='pil', image_mode='RGB')

    with gr.Row() :
        btn = gr.Button("Run", variant="primary")

    btn.click(fn=rgb_to_bgr, inputs=[input_img], outputs=[output_img])

demo.launch()
pngwn commented 4 days ago

Does gr.ImageMask() address this issue (if we fixed the opacity issue)?

gibiee commented 4 days ago

@pngwn gr.ImageMask() is essentially just gr.ImageEditor(brush=Brush(colors=['#000000'], color_mode='fixed')), but if the opacity issue is resolved, it could be a useful component for inpainting. However, since the inpainting feature does not require layers and the mask image does not need to be RGB, I think it could be simplified further.

pngwn commented 4 days ago

gr.ImageMask(layers=False) would be pretty close i think.

gibiee commented 4 days ago

@pngwn Yes, that is currently the method I often use. Come to think of it, if the opacity issue is resolved, controlling the brush color in RGB might be helpful. Using a bright brush color for dark images and a dark brush color for light images would make them more visible.