commit cbb297c88965eea20371f2b4019e8bc3da560bbd Author: SS-snap <2kganzaoji@gmail.com> Date: Mon Sep 9 21:01:05 2024 +0800 Initial commit diff --git a/area_calculator.py b/area_calculator.py new file mode 100644 index 0000000..43567f6 --- /dev/null +++ b/area_calculator.py @@ -0,0 +1,49 @@ +import torch + +class AreaCalculator: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "image": ("IMAGE",), + "color_choice": (["black", "white"],), + }, + } + + RETURN_TYPES = ("INT", "INT") + RETURN_NAMES = ("面积","占比%") + FUNCTION = "calculate_area" + CATEGORY = "Snap Processing" + + def calculate_area(self, image, color_choice): + + gray_image = torch.mean(image, dim=0) + + if color_choice == "black": + + mask = gray_image < 0.5 + else: + + mask = gray_image >= 0.5 + + + color_area = torch.sum(mask).item() + + + color_area = round(color_area) + + + total_area = gray_image.numel() + + + color_ratio = int((color_area / total_area) * 100) + + return (int(color_area), color_ratio) + +NODE_CLASS_MAPPINGS = { + "AreaCalculator": AreaCalculator +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "AreaCalculator": "Snap Area" +} \ No newline at end of file