@@ -72,6 +72,7 @@ class AnnotationPosition(Enum):
72
72
RIGHT = 2
73
73
TOP = 4
74
74
BOTTOM = 8
75
+ CENTER = 16
75
76
76
77
#: Top left position
77
78
TOP_LEFT = TOP | LEFT
@@ -85,6 +86,15 @@ class AnnotationPosition(Enum):
85
86
#: Bottom right position
86
87
BOTTOM_RIGHT = BOTTOM | RIGHT
87
88
89
+ #: Top center position
90
+ TOP_CENTER = TOP | CENTER
91
+
92
+ #: Middle of the screen position
93
+ CENTER_CENTER = CENTER | CENTER
94
+
95
+ #: Bottom center position
96
+ BOTTOM_CENTER = BOTTOM | CENTER
97
+
88
98
89
99
class ImageText : # pylint: disable=too-few-public-methods
90
100
"""ImageText represents some text that can be applied to an image.
@@ -156,15 +166,25 @@ def render(self, draw: ImageDraw.ImageDraw, bounds: tuple) -> ImageDraw.ImageDra
156
166
(bx1 , by1 , bx2 , by2 ) = bounds
157
167
text_width , text_height = draw .textsize (self .text , font = self .font )
158
168
159
- if self .position .value & AnnotationPosition .TOP .value :
160
- y = by1
169
+ if self .position .value == AnnotationPosition .CENTER_CENTER .value :
170
+ y = by1 + (by2 - by1 - (text_height ))/ 2
171
+ x = bx1 + (bx2 - bx1 - (text_width ))/ 2
161
172
else :
162
- y = by2 - text_height
173
+ if (self .position .value & AnnotationPosition .LEFT .value or self .position .value & AnnotationPosition .RIGHT .value )\
174
+ and self .position .value & AnnotationPosition .CENTER .value :
175
+ y = by1 + (by2 - by1 - (text_height )) / 2
176
+ if self .position .value & AnnotationPosition .TOP .value :
177
+ y = by1
178
+ else :
179
+ y = by2 - text_height
163
180
164
- if self .position .value & AnnotationPosition .LEFT .value :
165
- x = bx1
166
- else :
167
- x = bx2 - text_width
181
+ if (self .position .value & AnnotationPosition .TOP .value or self .position .value & AnnotationPosition .BOTTOM .value )\
182
+ and self .position .value & AnnotationPosition .CENTER .value :
183
+ x = bx1 + (bx2 - bx1 - (text_width )) / 2
184
+ elif self .position .value & AnnotationPosition .LEFT .value :
185
+ x = bx1
186
+ else :
187
+ x = bx2 - text_width
168
188
169
189
# helper method for each draw call below
170
190
def _draw_text (pos , color ):
0 commit comments