-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathborder.py
61 lines (49 loc) · 1.45 KB
/
border.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import cv2
import pathlib
import glob
import os
import numpy as np
from numpy.linalg import norm
OUTPUT_FOLDER = str(pathlib.Path("./output-border").resolve())
PHOTO_FOLDER = str(pathlib.Path("./output-colours").resolve())
photo_files = glob.glob(PHOTO_FOLDER + "/*")
color = [255, 255, 255] # white
top, bottom, left, right = [200, 200, 100, 100]
month_mappings = {
"01": "January",
"02": "February",
"03": "March",
"04": "April",
"05": "May",
"06": "June",
"07": "July",
"08": "August",
"09": "September",
"10": "October",
"11": "November",
"12": "December",
}
images = []
for photo_path in photo_files:
img = cv2.imread(photo_path)
images.append({
"path": photo_path,
"image": img
})
images.sort(key=lambda img: np.average(
norm(img["image"], axis=2)) / np.sqrt(3))
remove = 300
images = images[remove:]
for imageObject in images:
image = imageObject["image"]
path = imageObject["path"]
img = cv2.copyMakeBorder(
image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
filename = os.path.basename(path)
month_taken = month_mappings[filename[:2]]
font = cv2.FONT_HERSHEY_TRIPLEX
textsize = cv2.getTextSize(month_taken, font, 1, 2)[0]
textX = ((img.shape[1] - textsize[0]) // 2) - 60
cv2.putText(img, month_taken, (textX, 390), font,
2, (0, 0, 0), 2, cv2.LINE_AA)
cv2.imwrite(f"{OUTPUT_FOLDER}/{filename}", img)