-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageManipulationTransformation_Part02.py
More file actions
90 lines (71 loc) · 2.01 KB
/
ImageManipulationTransformation_Part02.py
File metadata and controls
90 lines (71 loc) · 2.01 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
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
82
83
84
85
86
87
88
89
90
# Basic Drawing Techniques
import cv2
img = cv2.imread("../Img/4.jpeg")
# Drawing a Line
h, w = img.shape[0], img.shape[1]
# vertical
linev = cv2.line(
img.copy(), (w // 2, 0), (w // 2, h), color=(255, 255, 255), thickness=50
)
cv2.imshow("vertical line img", linev)
cv2.waitKey(0)
cv2.destroyAllWindows()
# horizontal
lineh = cv2.line(
img.copy(), (0, h // 2), (w, h // 2), color=(255, 255, 255), thickness=50
)
cv2.imshow("horizontal line img", lineh)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("Vertical Line.jpeg", linev)
cv2.imwrite("Horizontal Line.jpeg", lineh)
# Drawing a Rectangle
rectangle = cv2.rectangle(
img.copy(), (1, 1), (w - 2, h - 2), color=(255, 0, 0), thickness=50
)
cv2.imshow("rectangle", rectangle)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("Rectangle.jpeg", rectangle)
# Drawing a circle
circle = cv2.circle(
img.copy(), center=(w // 2, h // 2), radius=150, color=(0, 0, 0), thickness=40
)
cv2.imshow("circle", circle)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("Circle.jpeg", circle)
# Adding text
text_img = cv2.putText(
img.copy(),
"Hi TEXT",
(0, h - 2),
cv2.FONT_HERSHEY_PLAIN,
fontScale=10.0,
color=(0, 255, 0),
thickness=15,
)
cv2.imshow("Text", text_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("TextImage.jpeg", text_img)
# More detailed way to add text
# Text settings
text = "Hi TEXT"
font = cv2.FONT_HERSHEY_PLAIN
font_scale = 10.0
thickness = 15
# Measure text size
(text_width, text_height), baseline = cv2.getTextSize(text, font, font_scale, thickness)
# Calculate centered position
x = (w - text_width) // 2
y = (h + text_height) // 2 # y is baseline-bottom, so add text height
# Draw the text
detailedtext_img = cv2.putText(
img.copy(), text, (x, y), font, font_scale, (0, 255, 0), thickness
)
# Show
cv2.imshow("Text", detailedtext_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("DetailedText.jpeg", detailedtext_img)