from .ImageManipulation import ImageManipulation
from ImageContainer import ImageContainer
from typing import Any, List
from utils.image_utils import pil_to_cv2, cv2_to_pil
import cv2


class BoxBlur(ImageManipulation):
    def getManipulationName(self) -> str:
        return "Box Blur"

    def getParameters(self) -> List[str]:
        return ["ksize"]

    def manipulateImage(self, image: ImageContainer, parameters: Any) -> None:
        if image is None or image.getImage() is None:
            return
        pil_img = image.getImage()
        cv_img = pil_to_cv2(pil_img)
        k = 15
        if isinstance(parameters, dict) and parameters.get("ksize"):
            k = int(parameters.get("ksize"))
        if k % 2 == 0:
            k += 1
        blurred = cv2.blur(cv_img, (k, k))
        image._imageData = cv2_to_pil(blurred)


