File: line.py

package info (click to toggle)
python-imgviz 1.7.6%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,056 kB
  • sloc: python: 3,131; makefile: 25
file content (25 lines) | stat: -rw-r--r-- 551 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import numpy as np
import PIL.Image
import PIL.ImageDraw

from .. import utils


def line(src, yx, fill, width=1):
    dst = utils.numpy_to_pillow(src)
    line_(img=dst, yx=yx, fill=fill, width=width)
    return utils.pillow_to_numpy(dst)


def line_(img, yx, fill, width=1):
    assert isinstance(img, PIL.Image.Image)
    yx = np.asarray(yx)
    assert yx.ndim == 2
    assert yx.shape[1] == 2
    fill = tuple(fill)

    draw = PIL.ImageDraw.Draw(img)

    xy = yx[:, ::-1]
    xy = xy.flatten().tolist()
    draw.line(xy, fill=fill, width=width)