| 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 star(src, center, size, fill=None, outline=None): |
| 12 |
"""Draw star 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 |
star_( |
| 35 |
img=dst, |
| 36 |
center=center, |
| 37 |
size=size, |
| 38 |
fill=fill, |
| 39 |
outline=outline, |
| 40 |
) |
| 41 |
return utils.pillow_to_numpy(dst) |
| 42 |
|
| 43 |
|
| 44 |
def star_(img, center, size, fill=None, outline=None): |
| 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.Draw(img) |
| 51 |
|
| 52 |
radius = size / 2 |
| 53 |
cy, cx = center |
| 54 |
|
| 55 |
|
| 56 |
angles_m = np.arange(0, 5) * np.pi * 2 / 5 + (np.pi / 2) |
| 57 |
x_m = cx + radius * np.cos(angles_m) |
| 58 |
y_m = cy - radius * np.sin(angles_m) |
| 59 |
xy_m = np.stack((x_m, y_m), axis=1) |
| 60 |
|
| 61 |
|
| 62 |
angles_v = angles_m + np.pi / 5 |
| 63 |
length = radius / (np.sin(np.pi / 5) / np.tan(np.pi / 10) + np.cos(np.pi / 10)) |
| 64 |
x_v = cx + length * np.cos(angles_v) |
| 65 |
y_v = cy - length * np.sin(angles_v) |
| 66 |
xy_v = np.stack((x_v, y_v), axis=1) |
| 67 |
|
| 68 |
xy = np.array( |
| 69 |
[ |
| 70 |
xy_m[0], |
| 71 |
xy_v[0], |
| 72 |
xy_m[1], |
| 73 |
xy_v[1], |
| 74 |
xy_m[2], |
| 75 |
xy_v[2], |
| 76 |
xy_m[3], |
| 77 |
xy_v[3], |
| 78 |
xy_m[4], |
| 79 |
xy_v[4], |
| 80 |
xy_m[0], |
| 81 |
] |
| 82 |
) |
| 83 |
xy = xy.flatten().tolist() |
| 84 |
draw.polygon(xy, fill=fill, outline=outline) |