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
32.21k stars 2.41k forks source link

ImageEditor cannot work as a mask editor as v3 Image #9265

Open selaselah opened 1 week ago

selaselah commented 1 week ago

in v3, mask editor can be implements by

Image(tool="sketch")

the mask value can to access via

value["mask"]

in v4, tool is replaces with ImageEditor

ImageEditor(brush=...)

The value of ImageEditor only contains

no mask (brush only) can be get. check changed pixel value is not working, because the color of pixels maybe same as brush.

A awful solution: user can create another layer, and draw mask into it. This solution is cumbersome and error-prone: the number of layer must exact 2; user must put image into layer#1 and draw brush into layer#2

I think add a "brushed" item to ImageEditor's value dict, which only contains the effect of all brush drawing, can solve this problem.

missionfloyd commented 1 week ago

A awful solution: user can create another layer, and draw mask into it. This solution is cumbersome and error-prone: the number of layer must exact 2; user must put image into layer#1 and draw brush into layer#2

The layers menu can be disabled with layers=False, so users can't make new layers.

import gradio as gr

demo = gr.Interface(
    fn=lambda img: img["layers"][0],
    inputs=[gr.ImageEditor(layers=False)],
    outputs=[gr.Image()],
)

demo.launch()
abidlabs commented 1 week ago

cc @pngwn in case we want to make this addition in v5?

pngwn commented 1 week ago

I don't think we need to add anything here.

Setting layers=False means the user can upload an image and draw a mask. The. You can use the background key for the bg image and the single layer as the mask layer.

Maybe we need to update the gr.ImageMask template to match this.