-
Notifications
You must be signed in to change notification settings - Fork 0
/
exam6-5.py
81 lines (61 loc) · 2.57 KB
/
exam6-5.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#-*- coding:utf-8 -*-
import argparse
import numpy as np
import cv2
parser = argparse.ArgumentParser(description="OpenCV Example")
parser.add_argument("--thick", default = 4, type=int, help="font thickness")
parser.add_argument("--scale", default = 4.0, type=float, help="font thickness")
args = parser.parse_args()
w = 640
h = 480
channel = 3
img = np.zeros((h, w, channel), np.uint8)
red = (0, 0, 255)
green = (0, 255, 0)
blue = (255, 0, 0)
white = (255, 255, 255)
yellow = (0, 255, 255)
cyan = (255, 255, 0)
magenta = (255, 0, 255)
x_leading = 20
y_leading = 20
def get_canvas_size(t, f):
y_pos = y_leading
y_size = 0
x_size = 0
(label_width, label_height), baseline = cv2.getTextSize(t, f, fontScale = args.scale, thickness = args.thick)
y_pos += (label_height + baseline + y_leading)
if(label_width > x_size - x_leading):
x_size = label_width + x_leading
if(y_pos > y_size):
y_size = y_pos
print('canvas size : W[%d] H[%d]'%(x_size, y_size))
img = np.zeros((y_size, x_size, channel), np.uint8)
return img
text = 'Hello OpenCV'
font = cv2.FONT_HERSHEY_COMPLEX
img = get_canvas_size(text, font)
y_pos = 0
thick = 4
(label_width, label_height), baseline = cv2.getTextSize(text, font, fontScale = args.scale, thickness = args.thick)
y_pos += label_height + baseline + y_leading
location = (20 - thick, y_pos)
cv2.putText(img, text, location, font, fontScale = args.scale, color = red, thickness = args.thick)
location = (20 + thick, y_pos)
cv2.putText(img, text, location, font, fontScale = args.scale, color = red, thickness = args.thick)
location = (20, y_pos - thick)
cv2.putText(img, text, location, font, fontScale = args.scale, color = red, thickness = args.thick)
location = (20, y_pos + thick)
cv2.putText(img, text, location, font, fontScale = args.scale, color = red, thickness = args.thick)
location = (20 - thick, y_pos - thick)
cv2.putText(img, text, location, font, fontScale = args.scale, color = red, thickness = args.thick)
location = (20 + thick, y_pos + thick)
cv2.putText(img, text, location, font, fontScale = args.scale, color = red, thickness = args.thick)
location = (20 - thick, y_pos - thick)
cv2.putText(img, text, location, font, fontScale = args.scale, color = red, thickness = args.thick)
location = (20 + thick, y_pos + thick)
cv2.putText(img, text, location, font, fontScale = args.scale, color = red, thickness = args.thick)
location = (20 , y_pos)
cv2.putText(img, text, location, font, fontScale = args.scale, color = white, thickness = args.thick)
cv2.imshow("drawing", img)
cv2.waitKey(0)