diff --git a/fourcell/normalize.py b/fourcell/normalize.py index 60a9fe3..e43d933 100644 --- a/fourcell/normalize.py +++ b/fourcell/normalize.py @@ -295,9 +295,6 @@ def create_blank (w, h, rgb_color = (255, 255, 255)) : blank[:] = color return blank -def place (bottom, top, x, y) : - return bottom.paste(top, (x, y)) - def get_mean_rect (holePunches) : left = 0 right = 0 @@ -308,7 +305,7 @@ def get_mean_rect (holePunches) : left += float(hp['x']) top += float(hp['y']) elif hp['order'] == 2 : - right += float(hp['y']) + right += float(hp['x']) top += float(hp['y']) elif hp['order'] == 3 : left += float(hp['x']) @@ -316,9 +313,9 @@ def get_mean_rect (holePunches) : elif hp['order'] == 5 : right += float(hp['x']) bottom += float(hp['y']) - x = round((right / 2.0) - (left / 2.0)) - y = round((bottom / 2.0) - (top / 2.0)) - return (x, y) + w = round((right / 2.0) - (left / 2.0)) + h = round((bottom / 2.0) - (top / 2.0)) + return (w, h) def center_within (larger, smaller) : @@ -328,7 +325,19 @@ def center_within (larger, smaller) : h2 = smaller[1] x = ((w1 - w2) / 2) y = ((h1 - h2) / 2) - return (x, y) + return (int(x), int(y)) + +# If we consider (0,0) as top left corner of image called +# im with left-to-right as x direction and top-to-bottom +# as y direction. and we have (x1,y1) as the top-left vertex +# and (x2,y2) as the bottom-right vertex of a rectangle +# region within that image, then: +# +# roi = im[y1:y2, x1:x2] + +def crop (img, xoffset, yoffset, w, h) : + #crop_img = img[y:y+h, x:x+w].copy() + return im[yoffset:yoffset+w, xoffset:xoffset+w].copy() if len(sys.argv) < 2: print('Please provide path of scan to normalize') @@ -358,6 +367,7 @@ normalHeight = round(float(width) / pageRatio) holePunches = find_hole_punches(img) rotated = correct_rotation(img, original, holePunches) +rotatedHeight, rotatedWidth = rotated.shape[:2] holePunches = find_hole_punches(rotated) blank = create_blank(width, normalHeight) @@ -378,11 +388,13 @@ print(f'Mean rectangle: {meanRect[0]},{meanRect[1]}') # offset is the position within the new normal image # the top left hole punch should be centered to offset = center_within((width, normalHeight), meanRect) -print(f'Offset: {offset[0]},{offset[1]}') +print(f'Offset : {offset[0]},{offset[1]}') +print(f'Topleft: {tl["x"]},{tl["y"]}') +print(f'Rotated: {rotatedWidth},{rotatedHeight}') +print(f'Blank : {width},{normalHeight}') -display(img) #normal = place(blank, rotated, 0, 0) -#display(normal) \ No newline at end of file +display(blank)