from .generate_main_image.image_deal_base_func import *


class GetMask(object):
    def __init__(self):
        pass

    def find_green_areas(self, cv2_jpg):
        # pp_jpg = PictureProcessing(image_path)
        # pp_jpg = pp_jpg.resize(value=800)
        # cv2_jpg = pil_to_cv2(pp_jpg.im)

        # 将图像从BGR格式转换为HSV格式
        hsv_image = cv2.cvtColor(cv2_jpg, cv2.COLOR_BGR2HSV)

        # 定义绿色范围
        lower_green = np.array([35, 43, 46])
        upper_green = np.array([77, 255, 255])

        # 根据颜色范围创建掩膜
        mask = cv2.inRange(hsv_image, lower_green, upper_green)
        # 查找轮廓
        contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        # 创建空白图像用于绘制符合条件的区域
        green_areas_mask = np.zeros_like(mask)
        for contour in contours:
            area = cv2.contourArea(contour)
            if area > 100:  # 只选择面积大于100的区域
                cv2.drawContours(green_areas_mask, [contour], -1, 255, -1)

        # 转换为PIL图像
        green_areas_mask_pil = Image.fromarray(green_areas_mask)
        # pp_bg = PictureProcessing("RGB", pp_jpg.size, (255, 255, 255))
        # bg_im = pp_bg.im
        # bg_im.paste(pp_jpg.im, mask=green_areas_mask_pil)
        return green_areas_mask_pil


if __name__ == '__main__':
    image_path = r"D:\MyDocuments\PythonCode\MyPython\red_dragonfly\deal_pics\auto_capture_V2\auto_photo\output\测试阴影\1\MD251202_6951（1）.jpg"
    # 使用函数并传入图片路径
    mask_image = GetMask().find_green_areas(image_path)
    mask_image.show()  # 显示结果
