Drawing on images¶
from interactive_pipe import draw — pure functions drawing detections,
keypoints, regions and labels on a copy of an image, keeping its dtype and
range. See Figures, heatmaps & overlays
for the conventions (coordinates, colours, speed).
Draw marks on images: points, boxes, lines, polygons, text and masks.
Small and dependency-free (numpy + Pillow, both required by interactive_pipe; OpenCV is not needed). Meant to be called inside a filter::
from interactive_pipe import draw
@interactive(threshold=(0.5, [0.0, 1.0]))
def detect(img, threshold=0.5):
boxes = find_boxes(img, threshold) # (N, 4) x0, y0, x1, y1
return draw.boxes(img, boxes, labels=[f"{s:.2f}" for s in scores])
Conventions shared by every function:
- Pure: the input image is never modified (pipeline inputs are read-only views); a new array is returned.
- dtype and range kept: float images (values in
[0, 1]),uint8(0-255) anduint16(0-65535) come back with the same dtype and range. Only the marked pixels change. - Channels: RGB and RGBA images keep their channels (an RGBA image gets
an opaque alpha where it is marked); grayscale
(H, W)/(H, W, 1)images are promoted to(H, W, 3)RGB, since marks are coloured. - Coordinates: pixels,
(x, y)order (x = column, y = row, origin at the top-left corner), sub-pixel values accepted. Marks partly or entirely outside the image are clipped, never an error; items with NaN coordinates are skipped. - Colours: a name (
"red","tab:blue"), a hex string ("#ff8800"), an(r, g, b)tuple of floats in[0, 1](or of ints in0-255when a value is above 1).color=Nonegives each item its own colour fromDEFAULT_COLORS(matplotlib'stab10cycle); a list of colours is used per item (cycled when shorter).
DEFAULT_COLORS
module-attribute
¶
DEFAULT_COLORS: List[Tuple[float, float, float]] = [(0.122, 0.467, 0.706), (1.0, 0.498, 0.055), (0.173, 0.627, 0.173), (0.839, 0.153, 0.157), (0.58, 0.404, 0.741), (0.549, 0.337, 0.294), (0.89, 0.467, 0.761), (0.498, 0.498, 0.498), (0.737, 0.741, 0.133), (0.09, 0.745, 0.812)]
points
¶
points(img: ndarray, xy: Any, color: ColorArg = None, radius: float = 3.0, labels: Optional[Sequence[Any]] = None, filled: bool = True, width: float = 1.0, text_size: Optional[float] = None) -> np.ndarray
Draw points (discs, or circles with filled=False) at xy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
image (not modified). |
required |
xy
|
Any
|
one |
required |
color
|
ColorArg
|
one colour, a list (one per point) or None for the colour cycle. |
None
|
radius
|
float
|
disc radius in pixels (a radius below 1 marks a single pixel). |
3.0
|
labels
|
Optional[Sequence[Any]]
|
optional text per point, drawn on the right of the point. |
None
|
filled
|
bool
|
discs (True) or circle outlines of |
True
|
text_size
|
Optional[float]
|
label height in pixels (default from the image size). |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
A new image with the points drawn. |
Source code in src/interactive_pipe/draw.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
boxes
¶
boxes(img: ndarray, boxes: Any, labels: Optional[Sequence[Any]] = None, color: ColorArg = None, width: float = 2.0, fmt: str = 'xyxy', text_size: Optional[float] = None) -> np.ndarray
Draw rectangles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
image (not modified). |
required |
boxes
|
Any
|
one box or an |
required |
labels
|
Optional[Sequence[Any]]
|
optional text per box, drawn on a filled tag above the box (inside it when the box touches the top of the image). |
None
|
color
|
ColorArg
|
one colour, a list (one per box) or None for the colour cycle. |
None
|
width
|
float
|
line width in pixels. |
2.0
|
fmt
|
str
|
|
'xyxy'
|
text_size
|
Optional[float]
|
label height in pixels (default from the image size). |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
A new image with the boxes drawn. |
Source code in src/interactive_pipe/draw.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |
lines
¶
lines(img: ndarray, lines: Any, color: ColorArg = None, width: float = 2.0, closed: bool = False) -> np.ndarray
Draw polylines or segments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
image (not modified). |
required |
lines
|
Any
|
one polyline |
required |
color
|
ColorArg
|
one colour, a list (one per polyline) or None for the colour cycle. |
None
|
width
|
float
|
line width in pixels. |
2.0
|
closed
|
bool
|
join the last vertex back to the first. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
A new image with the lines drawn. |
Source code in src/interactive_pipe/draw.py
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 | |
polygon
¶
polygon(img: ndarray, polygons: Any, color: ColorArg = None, width: float = 2.0, fill_alpha: float = 0.0) -> np.ndarray
Draw closed polygons, optionally filled with a translucent colour.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
image (not modified). |
required |
polygons
|
Any
|
one polygon |
required |
color
|
ColorArg
|
one colour, a list (one per polygon) or None for the colour cycle. |
None
|
width
|
float
|
outline width in pixels (0: no outline, only the fill). |
2.0
|
fill_alpha
|
float
|
opacity of the fill, 0 (outline only, default) to 1. |
0.0
|
Returns:
| Type | Description |
|---|---|
ndarray
|
A new image with the polygons drawn. |
Source code in src/interactive_pipe/draw.py
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | |
text
¶
text(img: ndarray, text: Union[str, Sequence[str]], xy: Any, color: ColorArg = 'white', size: Optional[float] = None, anchor: str = 'top-left', outline: Optional[Color] = 'black', background: Optional[Color] = None, background_alpha: float = 0.6) -> np.ndarray
Write text, readable on any image.
By default the text is white with a thin black outline, readable on dark
and bright areas alike; background=<colour> puts it on a translucent
box instead (outline=None to drop the outline).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
image (not modified). |
required |
text
|
Union[str, Sequence[str]]
|
one string, or a list of strings (one per position in |
required |
xy
|
Any
|
|
required |
color
|
ColorArg
|
text colour (one, a list, or None for the colour cycle). |
'white'
|
size
|
Optional[float]
|
text height in pixels (default from the image size, >= 12). |
None
|
anchor
|
str
|
which point of the text sits at |
'top-left'
|
outline
|
Optional[Color]
|
colour of the outline around the letters, None for none. |
'black'
|
background
|
Optional[Color]
|
colour of a box behind the text, None for none. |
None
|
background_alpha
|
float
|
opacity of that box. |
0.6
|
Returns:
| Type | Description |
|---|---|
ndarray
|
A new image with the text written. Multi-line strings are supported. |
Source code in src/interactive_pipe/draw.py
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | |
mask
¶
mask(img: ndarray, mask: ndarray, color: ColorArg = None, alpha: float = 0.5) -> np.ndarray
Blend a mask over the image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
image (not modified). |
required |
mask
|
ndarray
|
|
required |
color
|
ColorArg
|
see above. |
None
|
alpha
|
float
|
opacity of the mask colour, 0 to 1. |
0.5
|
Returns:
| Type | Description |
|---|---|
ndarray
|
A new image with the mask blended in. |
Source code in src/interactive_pipe/draw.py
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 | |
to_rgb
¶
to_rgb(color: Color) -> Tuple[float, float, float]
(r, g, b) floats in [0, 1] from a colour name, a hex string or
a tuple (floats in [0, 1], or ints in 0-255 when a value is > 1).
Source code in src/interactive_pipe/draw.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |