Opencv-solver/Sudoku_extractor.py
def scale_and_centre(img, size, margin=0, background=0):
        """Scales and centres an image onto a new background square."""
        h, w = img.shape[:2]

        def centre_pad(length):
            """Handles centering for a given length that may be odd or even."""
            if length % 2 == 0:
                side1 = int((size - length) / 2)
                side2 = side1
            else:
                side1 = int((size - length) / 2)
                side2 = side1 + 1
            return side1, side2

        def scale(r, x):
            return int(r * x)

        if h > w:
            t_pad = int(margin / 2)
            b_pad = t_pad
            ratio = (size - margin) / h
            w, h = scale(ratio, w), scale(ratio, h)
            l_pad, r_pad = centre_pad(w)
        else:
            l_pad = int(margin / 2)
            r_pad = l_pad
            ratio = (size - margin) / w
            w, h = scale(ratio, w), scale(ratio, h)
            t_pad, b_pad = centre_pad(h)

        img = cv2.resize(img, (w, h))
        img = cv2.copyMakeBorder(img, t_pad, b_pad, l_pad, r_pad, cv2.BORDER_CONSTANT, None, background)
        return cv2.resize(img, (size, size))
Scales and centres an image onto a new background square.
Scales and centres an image onto a new background square.
Scales and centres an image onto a new background square .
def scale_and_centre(img, size, margin=0, background=0):
        h, w = img.shape[:2]
        def centre_pad(length):
            if length % 2 == 0:
                side1 = int((size - length) / 2)
                side2 = side1
            else:
                side1 = int((size - length) / 2)
                side2 = side1 + 1
            return side1, side2
        def scale(r, x):
            return int(r * x)
        if h > w:
            t_pad = int(margin / 2)
            b_pad = t_pad
            ratio = (size - margin) / h
            w, h = scale(ratio, w), scale(ratio, h)
            l_pad, r_pad = centre_pad(w)
        else:
            l_pad = int(margin / 2)
            r_pad = l_pad
            ratio = (size - margin) / w
            w, h = scale(ratio, w), scale(ratio, h)
            t_pad, b_pad = centre_pad(h)
        img = cv2.resize(img, (w, h))
        img = cv2.copyMakeBorder(img, t_pad, b_pad, l_pad, r_pad, cv2.BORDER_CONSTANT, None, background)
        return cv2.resize(img, (size, size))
def scale_and_centre ( img , size , margin = 0 , background = 0 ) : h , w = img . shape [ : 2 ] def centre_pad ( length ) : """Handles centering for a given length that may be odd or even.""" if length % 2 == 0 : side1 = int ( ( size - length ) / 2 ) side2 = side1 else : side1 = int ( ( size - length ) / 2 ) side2 = side1 + 1 return side1 , side2 def scale ( r , x ) : return int ( r * x ) if h > w : t_pad = int ( margin / 2 ) b_pad = t_pad ratio = ( size - margin ) / h w , h = scale ( ratio , w ) , scale ( ratio , h ) l_pad , r_pad = centre_pad ( w ) else : l_pad = int ( margin / 2 ) r_pad = l_pad ratio = ( size - margin ) / w w , h = scale ( ratio , w ) , scale ( ratio , h ) t_pad , b_pad = centre_pad ( h ) img = cv2 . resize ( img , ( w , h ) ) img = cv2 . copyMakeBorder ( img , t_pad , b_pad , l_pad , r_pad , cv2 . BORDER_CONSTANT , None , background ) return cv2 . resize ( img , ( size , size ) )
Scales and centres an image onto a new background square.
Scales and centres an image onto a new background square .
"""Scales and centres an image onto a new background square.""" """Handles centering for a given length that may be odd or even."""
img
size
margin
background