Вот соответствующая часть моего кода:
Код: Выделить всё
import gradio as gr
from gradio.components.image_editor import Brush
import numpy as np
from PIL import Image
brush = Brush(colors=["#FFFFFF"], color_mode="fixed")
last_input_mask = None
last_input_img = None
def extract_input(input_image):
global last_input_mask
global last_input_img
last_input_mask = input_image["layers"][0]
last_input_img = input_image["background"]
def update_masked_img():
global last_input_mask
global last_input_img
image = last_input_img
mask = last_input_mask
masked_img = Image.fromarray(np.array(mask).astype(np.uint8))
return masked_img, image, mask
def image_again(img):
return img
if __name__ == "__main__":
with gr.Blocks() as demo:
with gr.Row():
with gr.Column(show_progress=True):
input_img = gr.ImageEditor(
label="Input Image",
layers=False,
brush=brush,
show_label=True,
width=700
)
masked_img_btn = gr.Button("Put brush to 'Masked Image'")
split_btn = gr.Button("RETURN AGAIN")
with gr.Column(show_progress=True):
masked_img = gr.Image(label="Masked Image", interactive=False, container=True, type="numpy", width=700)
scale = gr.Slider(0.0,1.0, value=0.6, label="Scale")
mask = gr.Image(label="Input Mask", interactive=False, container=True, type="pil", visible=False)
image = gr.Image(label="Input image", interactive=False, container=True, type="pil", visible=False)
input_img.change(
fn=extract_input,
inputs=[input_img],
)
masked_img_btn.click(
fn=update_masked_img,
outputs=[masked_img, image, mask],
)
split_btn.click(
fn=image_again,
inputs=[masked_img],
outputs=[masked_img]
)
demo.launch(debug=True, share=False)

Подробнее здесь: https://stackoverflow.com/questions/788 ... brush-tool