| 1 | import collections |
| 2 | |
| 3 | import PIL.Image |
| 4 | |
| 5 | from .. import utils |
| 6 | |
| 7 | |
| 8 | def rectangle(src, aabb1, aabb2, fill=None, outline=None, width=0): |
| 9 | """Draw rectangle on numpy array with Pillow. |
| 10 | |
| 11 | Parameters |
| 12 | ---------- |
| 13 | src: numpy.ndarray |
| 14 | Input image. |
| 15 | aabb1: array-like, (2,) |
| 16 | Minimum vertex (y_min, x_min) of the axis aligned bounding box (AABB). |
| 17 | aabb2: array-like, (2,) |
| 18 | Maximum vertex (y_max, x_max) of the AABB. |
| 19 | fill: int or array-like, (3,), optional |
| 20 | RGB color to fill the mark. None for no fill. (default: None) |
| 21 | outline: int or array-like, (3,), optional |
| 22 | RGB color to draw the outline. |
| 23 | width: int, optional |
| 24 | Rectangle line width. (default: 0) |
| 25 | |
| 26 | Returns |
| 27 | ------- |
| 28 | dst: numpy.ndarray |
| 29 | Output image. |
| 30 | |
| 31 | """ |
| 32 | dst = utils.numpy_to_pillow(src) |
| 33 | rectangle_( |
| 34 | img=dst, |
| 35 | aabb1=aabb1, |
| 36 | aabb2=aabb2, |
| 37 | fill=fill, |
| 38 | outline=outline, |
| 39 | width=width, |
| 40 | ) |
| 41 | return utils.pillow_to_numpy(dst) |
| 42 | |
| 43 | |
| 44 | def rectangle_(img, aabb1, aabb2, fill=None, outline=None, width=0): |
| 45 | if isinstance(fill, collections.abc.Iterable): |
| 46 | fill = tuple(fill) |
| 47 | if isinstance(outline, collections.abc.Iterable): |
| 48 | outline = tuple(outline) |
| 49 | |
| 50 | draw = PIL.ImageDraw.ImageDraw(img) |
| 51 | |
| 52 | y1, x1 = aabb1 |
| 53 | y2, x2 = aabb2 |
| 54 | draw.rectangle(xy=(x1, y1, x2, y2), fill=fill, outline=outline, width=width) |