-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_rotated_images.py
More file actions
27 lines (24 loc) · 1.13 KB
/
Copy pathmake_rotated_images.py
File metadata and controls
27 lines (24 loc) · 1.13 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
import cv2
import numpy as np
import os
from pdf2image import convert_from_path
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('file')
args = parser.parse_args()
outname, ext = os.path.splitext(args.file)
images = convert_from_path(args.file, dpi=200, fmt='jpg', grayscale=False, output_folder='work')
cv_image = cv2.cvtColor(np.array(images[0]), cv2.COLOR_RGB2BGR)
for angle in [0., 1., 2., 5., 10., 15., 20, 31., 46., 87., 92., 140., 170.]:
w = cv_image.shape[1]
h = cv_image.shape[0]
hn = max(int(h * abs(np.cos(np.pi *angle/180.)) + w * abs(np.sin(np.pi *angle/180.))), h)
wn = max(int(w * abs(np.cos(np.pi *angle/180.)) + h * abs(np.sin(np.pi *angle/180.))), w)
hs = (hn - h)//2
ws = (wn - w)//2
img = 255*np.ones((hn, wn, 3))
img[hs:h+hs, ws:w+ws, :] = cv_image
M = cv2.getRotationMatrix2D((wn // 2, hn // 2), angle, 1.0)
rotated = cv2.warpAffine(img, M, (wn, hn), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
cv2.imwrite('{}_{:03.0f}.jpg'.format(outname, angle),rotated)