region:us
import gradio as gr
import numpy as np
from PIL import Image, ImageDraw

def generate_mask(image, mask_points):
    # 创建一个与原始图像相同大小的空白图像
    mask = Image.new('L', image.size, 0)
    draw = ImageDraw.Draw(mask)
    
    # 绘制多边形蒙版
    draw.polygon(mask_points, fill=255)
    
    # 将蒙版应用到原始图像
    masked_image = Image.new('RGBA', image.size)
    masked_image.paste(image, (0, 0), mask)
    
    return masked_image

def generate_masked_image(image, mask_points):
    masked_image = generate_mask(image, mask_points)
    return masked_image

def generate_downloadable_image(image, mask_points):
    masked_image = generate_mask(image, mask_points)
    return masked_image

def image_to_array(image):
    return np.array(image)

def array_to_image(array):
    return Image.fromarray(np.uint8(array))

iface = gr.Interface(
    fn=generate_masked_image, 
    inputs=[
        gr.inputs.Image(label="原始图像"), 
        gr.inputs.ImageDraw(label="绘制蒙版", drawing_mode="freedraw", outputs="points")
    ], 
    outputs=gr.outputs.Image(label="蒙版图像")
)

iface.launch()