| 1 | import numpy as np |
| 2 | |
| 3 | from .. import color as color_module |
| 4 | from .. import utils |
| 5 | from .rectangle import rectangle_ |
| 6 | from .text import text_ |
| 7 | from .text import text_size |
| 8 | |
| 9 | |
| 10 | def text_in_rectangle_aabb(img_shape, loc, text, size, aabb1, aabb2, font_path=None): |
| 11 | height, width = img_shape[:2] |
| 12 | |
| 13 | y1, x1 = (0, 0) if aabb1 is None else aabb1 |
| 14 | y2, x2 = (height - 1, width - 1) if aabb2 is None else aabb2 |
| 15 | |
| 16 | tsize = text_size(text, size, font_path=font_path) |
| 17 | |
| 18 | if loc == "lt": |
| 19 | yx = (y1, x1) |
| 20 | elif loc == "lt+": |
| 21 | yx = (y1 - tsize[0] - 2, x1) |
| 22 | elif loc == "rt": |
| 23 | yx = (y1, x2 - tsize[1] - 2) |
| 24 | elif loc == "rt+": |
| 25 | yx = (y1 - tsize[0] - 2, x2 - tsize[1] - 2) |
| 26 | elif loc == "lb": |
| 27 | yx = (y2 - tsize[0] - 2, 0) |
| 28 | elif loc == "lb-": |
| 29 | yx = (y2, 0) |
| 30 | elif loc == "rb": |
| 31 | yx = (y2 - tsize[0] - 2, x2 - tsize[1] - 2) |
| 32 | elif loc == "rb-": |
| 33 | yx = (y2, x2 - tsize[1] - 2) |
| 34 | else: |
| 35 | raise ValueError("unsupported loc: {}".format(loc)) |
| 36 | |
| 37 | y1, x1 = yx |
| 38 | y2, x2 = y1 + tsize[0] + 1, x1 + tsize[1] + 1 |
| 39 | |
| 40 | return np.array([y1, x1, y2, x2]) |
| 41 | |
| 42 | |
| 43 | def text_in_rectangle( |
| 44 | src, |
| 45 | loc, |
| 46 | text, |
| 47 | size, |
| 48 | background, |
| 49 | color=None, |
| 50 | aabb1=None, |
| 51 | aabb2=None, |
| 52 | font_path=None, |
| 53 | keep_size=False, |
| 54 | ): |
| 55 | """Draw text in a rectangle. |
| 56 | |
| 57 | Parameters |
| 58 | ---------- |
| 59 | src: numpy.ndarray |
| 60 | Input image. |
| 61 | loc: str |
| 62 | Location of text. It must be one of following: |
| 63 | lt, rt, lb, rb, lt+, rt+, lb-, rb-. |
| 64 | text: str |
| 65 | Text to draw. |
| 66 | size: int |
| 67 | Text size in pixel. |
| 68 | background: (3,) array-like |
| 69 | Background color in uint8. |
| 70 | color: (3,) array-like |
| 71 | Text RGB color in uint8. |
| 72 | If None, the color is determined by background color. |
| 73 | (default: None) |
| 74 | aabb1, aabb2: (2,) array-like |
| 75 | Coordinate of the rectangle (y_min, x_min), (y_max, x_max). |
| 76 | Default is (0, 0), (height, width). |
| 77 | keep_size: bool |
| 78 | Force to keep original size (size change happens with loc=xx+, xx-). |
| 79 | |
| 80 | Returns |
| 81 | ------- |
| 82 | dst: numpy.ndarray |
| 83 | Output image. |
| 84 | |
| 85 | """ |
| 86 | if color is None: |
| 87 | color = color_module.get_fg_color(background) |
| 88 | |
| 89 | height, width = src.shape[:2] |
| 90 | y1, x1, y2, x2 = text_in_rectangle_aabb( |
| 91 | img_shape=src.shape, |
| 92 | loc=loc, |
| 93 | text=text, |
| 94 | size=size, |
| 95 | aabb1=aabb1, |
| 96 | aabb2=aabb2, |
| 97 | font_path=font_path, |
| 98 | ) |
| 99 | |
| 100 | dst = src |
| 101 | |
| 102 | if not keep_size: |
| 103 | constant_values = ( |
| 104 | (background[0],), |
| 105 | (background[1],), |
| 106 | (background[2],), |
| 107 | ) |
| 108 | if y1 < 0: |
| 109 | pad = -y1 |
| 110 | dst = np.pad( |
| 111 | dst, |
| 112 | ((pad, 0), (0, 0), (0, 0)), |
| 113 | mode="constant", |
| 114 | constant_values=constant_values, |
| 115 | ) |
| 116 | y1 += pad |
| 117 | y2 += pad |
| 118 | if y2 > height: |
| 119 | pad = y2 - height |
| 120 | dst = np.pad( |
| 121 | dst, |
| 122 | ((0, pad), (0, 0), (0, 0)), |
| 123 | mode="constant", |
| 124 | constant_values=constant_values, |
| 125 | ) |
| 126 | |
| 127 | dst = utils.numpy_to_pillow(dst) |
| 128 | rectangle_( |
| 129 | img=dst, |
| 130 | aabb1=(y1, x1), |
| 131 | aabb2=(y2, x2), |
| 132 | fill=background, |
| 133 | ) |
| 134 | text_( |
| 135 | img=dst, |
| 136 | yx=(y1 + 1, x1 + 1), |
| 137 | text=text, |
| 138 | color=color, |
| 139 | size=size, |
| 140 | font_path=font_path, |
| 141 | ) |
| 142 | return utils.pillow_to_numpy(dst) |
| 143 | |
| 144 | |
| 145 | def text_in_rectangle_( |
| 146 | img, |
| 147 | loc, |
| 148 | text, |
| 149 | size, |
| 150 | background, |
| 151 | color=None, |
| 152 | aabb1=None, |
| 153 | aabb2=None, |
| 154 | font_path=None, |
| 155 | ): |
| 156 | if color is None: |
| 157 | color = color_module.get_fg_color(background) |
| 158 | |
| 159 | y1, x1, y2, x2 = text_in_rectangle_aabb( |
| 160 | img_shape=(img.height, img.width), |
| 161 | loc=loc, |
| 162 | text=text, |
| 163 | size=size, |
| 164 | aabb1=aabb1, |
| 165 | aabb2=aabb2, |
| 166 | font_path=font_path, |
| 167 | ) |
| 168 | rectangle_( |
| 169 | img=img, |
| 170 | aabb1=(y1, x1), |
| 171 | aabb2=(y2, x2), |
| 172 | fill=background, |
| 173 | ) |
| 174 | text_( |
| 175 | img=img, |
| 176 | yx=(y1 + 1, x1 + 1), |
| 177 | text=text, |
| 178 | color=color, |
| 179 | size=size, |
| 180 | font_path=font_path, |
| 181 | ) |