| 1 | import collections |
| 2 | |
| 3 | import PIL.Image |
| 4 | import PIL.ImageDraw |
| 5 | import PIL.ImageFont |
| 6 | |
| 7 | from .. import utils |
| 8 | |
| 9 | |
| 10 | def circle(src, center, diameter, fill=None, outline=None, width=0): |
| 11 | """Draw circle on numpy array with Pillow. |
| 12 | |
| 13 | Parameters |
| 14 | ---------- |
| 15 | src: numpy.ndarray |
| 16 | Input image. |
| 17 | center: (2,) array-like |
| 18 | center is (cy, cx). |
| 19 | diameter: float |
| 20 | Diameter of the circle. |
| 21 | fill: int or (3,) array-like, optional |
| 22 | RGB color to fill the mark. None for no fill. (default: None) |
| 23 | outline: int or (3,) array-like, optional |
| 24 | RGB color to draw the outline. |
| 25 | width: int, optional |
| 26 | Rectangle line width. (default: 0) |
| 27 | |
| 28 | Returns |
| 29 | ------- |
| 30 | dst: numpy.ndarray |
| 31 | Output image. |
| 32 | |
| 33 | """ |
| 34 | dst = utils.numpy_to_pillow(src) |
| 35 | circle_( |
| 36 | img=dst, |
| 37 | center=center, |
| 38 | diameter=diameter, |
| 39 | fill=fill, |
| 40 | outline=outline, |
| 41 | width=width, |
| 42 | ) |
| 43 | return utils.pillow_to_numpy(dst) |
| 44 | |
| 45 | |
| 46 | def circle_(img, center, diameter, fill=None, outline=None, width=0): |
| 47 | if isinstance(fill, collections.abc.Iterable): |
| 48 | fill = tuple(fill) |
| 49 | if isinstance(outline, collections.abc.Iterable): |
| 50 | outline = tuple(outline) |
| 51 | |
| 52 | draw = PIL.ImageDraw.Draw(img) |
| 53 | |
| 54 | cy, cx = center |
| 55 | |
| 56 | radius = diameter / 2.0 |
| 57 | x1 = cx - radius |
| 58 | x2 = x1 + diameter |
| 59 | y1 = cy - radius |
| 60 | y2 = y1 + diameter |
| 61 | |
| 62 | draw.ellipse([x1, y1, x2, y2], fill=fill, outline=outline, width=width) |