Skip to content

Commit

Permalink
Allow passing trace options directly as kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonyte committed Nov 12, 2023
1 parent 2191941 commit 1767241
Showing 1 changed file with 81 additions and 2 deletions.
83 changes: 81 additions & 2 deletions autotrace/autotrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
from dataclasses import dataclass
from enum import Enum, IntEnum
from typing import TYPE_CHECKING, Sequence
from typing import TYPE_CHECKING, Sequence, overload

if TYPE_CHECKING:
from os import PathLike
Expand Down Expand Up @@ -216,7 +216,64 @@ def __init__(
def __len__(self) -> int:
return len(self.data)

def trace(self, options: TraceOptions | None = None) -> Vector:
@overload
# pylint: disable=too-many-arguments, too-many-locals
def trace( # noqa: PLR0913
self,
*,
background_color: Color | None = None,
charcode: int = 0,
color_count: int = 0,
corner_always_threshold: float = 60.0,
corner_surround: int = 4,
corner_threshold: float = 100.0,
error_threshold: float = 2.0,
filter_iterations: int = 4,
line_reversion_threshold: float = 0.01,
line_threshold: float = 1.0,
remove_adjacent_corners: bool = False,
tangent_surround: int = 3,
despeckle_level: int = 0,
despeckle_tightness: float = 2.0,
noise_removal: float = 0.99,
centerline: bool = False,
preserve_width: bool = False,
width_weight_factor: float = 6.0,
) -> Vector:
...

@overload
def trace(
self,
*,
options: TraceOptions,
) -> Vector:
...

# pylint: disable=too-many-arguments, too-many-locals
def trace( # noqa: PLR0913
self,
*,
background_color: Color | None = None,
charcode: int = 0,
color_count: int = 0,
corner_always_threshold: float = 60.0,
corner_surround: int = 4,
corner_threshold: float = 100.0,
error_threshold: float = 2.0,
filter_iterations: int = 4,
line_reversion_threshold: float = 0.01,
line_threshold: float = 1.0,
remove_adjacent_corners: bool = False,
tangent_surround: int = 3,
despeckle_level: int = 0,
despeckle_tightness: float = 2.0,
noise_removal: float = 0.99,
centerline: bool = False,
preserve_width: bool = False,
width_weight_factor: float = 6.0,
options: TraceOptions | None = None,
) -> Vector:
"""Trace a vector from the bitmap.
Args:
Expand All @@ -226,6 +283,28 @@ def trace(self, options: TraceOptions | None = None) -> Vector:
Vector: The traced vector image.
"""

if options is None:
options = TraceOptions(
background_color=background_color,
charcode=charcode,
color_count=color_count,
corner_always_threshold=corner_always_threshold,
corner_surround=corner_surround,
corner_threshold=corner_threshold,
error_threshold=error_threshold,
filter_iterations=filter_iterations,
line_reversion_threshold=line_reversion_threshold,
line_threshold=line_threshold,
remove_adjacent_corners=remove_adjacent_corners,
tangent_surround=tangent_surround,
despeckle_level=despeckle_level,
despeckle_tightness=despeckle_tightness,
noise_removal=noise_removal,
centerline=centerline,
preserve_width=preserve_width,
width_weight_factor=width_weight_factor,
)

return _trace(self.data, options)


Expand Down

0 comments on commit 1767241

Please sign in to comment.