| 1 | import collections |
| 2 | |
| 3 | import numpy as np |
| 4 | import PIL.Image |
| 5 | import PIL.ImageDraw |
| 6 | import PIL.ImageFont |
| 7 | |
| 8 | from .. import utils |
| 9 | |
| 10 | |
| 11 | def triangle(src, center, size, fill=None, outline=None): |
| 12 | """Draw triangle on numpy array with Pillow. |
| 13 | |
| 14 | Parameters |
| 15 | ---------- |
| 16 | src: numpy.ndarray |
| 17 | Input image. |
| 18 | center: (2,) array-like |
| 19 | center is (cy, cx). |
| 20 | size: float |
| 21 | Diameter to create the star. |
| 22 | fill: int or (3,) array-like, optional |
| 23 | RGB color to fill the mark. None for no fill. (default: None) |
| 24 | outline: int or (3,) array-like, optional |
| 25 | RGB color to draw the outline. |
| 26 | |
| 27 | Returns |
| 28 | ------- |
| 29 | dst: numpy.ndarray |
| 30 | Output image. |
| 31 | |
| 32 | """ |
| 33 | dst = utils.numpy_to_pillow(src) |
| 34 | triangle_(img=dst, center=center, size=size, fill=fill, outline=outline) |
| 35 | return utils.pillow_to_numpy(dst) |
| 36 | |
| 37 | |
| 38 | def triangle_(img, center, size, fill=None, outline=None): |
| 39 | if isinstance(fill, collections.abc.Iterable): |
| 40 | fill = tuple(fill) |
| 41 | if isinstance(outline, collections.abc.Iterable): |
| 42 | outline = tuple(outline) |
| 43 | |
| 44 | draw = PIL.ImageDraw.Draw(img) |
| 45 | |
| 46 | radius = size / 2 |
| 47 | cy, cx = center |
| 48 | |
| 49 | x = cx + radius * np.cos(np.deg2rad(np.arange(0, 3) * 120 + 90)) |
| 50 | y = cy - radius * np.sin(np.deg2rad(np.arange(0, 3) * 120 + 90)) |
| 51 | |
| 52 | xy = np.stack((x, y), axis=1) |
| 53 | xy = xy.flatten().tolist() |
| 54 | draw.polygon(xy, fill=fill, outline=outline) |