from technique import AlteringTechnique
import cv2

class cropAugmentationTechnique(AlteringTechnique):

    # percentage is a value between 0 and 1
    # startFrom indicates the starting point of the cropping,
    # the possible values are TOPLEFT, TOPRIGHT, BOTTOMLEFT,
    # BOTTOMRIGHT, and CENTER
    def __init__(self,parameters):
        AlteringTechnique.__init__(self, parameters)
        if 'percentage' in parameters.keys():
            self.percentage = float(parameters["percentage"])
        else:
            self.percentage = 0.9
        if 'startFrom' in parameters.keys():
            self.startFrom = parameters["startFrom"]
        else:
            self.startFrom = 'TOPLEFT'

        if (self.percentage <0 or self.percentage >1):
            raise NameError("Invalid value for cropping")
        if self.startFrom not in ['TOPLEFT', 'TOPRIGHT', 'BOTTOMLEFT', 'BOTTOMRIGHT', 'CENTER']:
            raise NameError("Invalid value for cropping")


    def apply(self, image):
        (w,h) = image.shape[:2]
        newW = int(w*self.percentage)
        newH = int(h * self.percentage)
        if self.startFrom == 'TOPLEFT':
            crop = image[0:newW,0:newH]
        if self.startFrom == 'BOTTOMLEFT':
            crop = image[w-newW:w, 0:newH]
        if self.startFrom == 'TOPRIGHT':
            crop = image[0:newW,h-newH:h]
        if self.startFrom == 'BOTTOMRIGHT':
            crop = image[w-newW:w, h - newH:h]
        if self.startFrom == 'CENTER':
            crop = image[int(w/2 - newW/2):int(w/2 + newW/2), int(h/2 - newH/2):int(h/2 + newH/2)]
        return crop


# Example
# technique = cropAugmentationTechnique(0.5,'TOPRIGHT')
# image = cv2.imread("LPR1.jpg")
# cv2.imshow("original",image)
# cv2.imshow("new",technique.apply(image))
# cv2.waitKey(0)