-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathangle_correction.py
More file actions
41 lines (30 loc) · 1.34 KB
/
angle_correction.py
File metadata and controls
41 lines (30 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import math
from typing import Tuple, Union
import cv2
import numpy as np
from deskew import determine_skew
import os
import shutil
def rotate(
image: np.ndarray, angle: float, background: Union[int, Tuple[int, int, int]]
) -> np.ndarray:
old_width, old_height = image.shape[:2]
angle_radian = math.radians(angle)
width = abs(np.sin(angle_radian) * old_height) + abs(np.cos(angle_radian) * old_width)
height = abs(np.sin(angle_radian) * old_width) + abs(np.cos(angle_radian) * old_height)
image_center = tuple(np.array(image.shape[1::-1]) / 2)
rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
rot_mat[1, 2] += (width - old_width) / 2
rot_mat[0, 2] += (height - old_height) / 2
return cv2.warpAffine(image, rot_mat, (int(round(height)), int(round(width))), borderValue=background)
def image_deskew(input_folder, output_folder):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
image_files = os.listdir(input_folder)
for image_file in image_files:
image_path = os.path.join(input_folder, image_file)
output_path = os.path.join(output_folder, image_file)
shutil.copy(image_path, output_path)
input_folder = 'place_docs_here/processing/pic_to_png'
output_folder = 'place_docs_here/processing/rotated_pngs'
image_deskew(input_folder, output_folder)