Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions vectorizing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def process_binary(img):
solver = BinarySolver(img)
return solver.solve()

def process_color(img, color_count, timer):
solver = ColorSolver(img, color_count, timer)
def process_color(img, color_count, tolerance, timer):
solver = ColorSolver(img, color_count, tolerance, timer)
return solver.solve()

def validate_args(args):
Expand All @@ -56,12 +56,17 @@ def validate_args(args):
if not only_numbers:
return False

tolerance = args.get("tolerance")
if tolerance is not None and tolerance < 0:
return False

return SimpleNamespace(
crop_box = box,
solver = solver,
url = args.get('url'),
raw = args.get('raw'),
color_count = args.get('color_count'),
crop_box=box,
solver=solver,
url=args.get("url"),
raw=args.get("raw"),
color_count=args.get("color_count"),
tolerance=args.get("tolerance"),
)

def invalid_args():
Expand All @@ -87,6 +92,10 @@ def index():
color_count = args.color_count
raw = args.raw
crop_box = args.crop_box
tolerance = args.tolerance

if tolerance is None:
tolerance = 0.2

try:
timer = Timer()
Expand All @@ -105,7 +114,7 @@ def index():

else:
timer.start_timer('Color Solver - Total')
solved = process_color(img, color_count, timer)
solved = process_color(img, color_count, tolerance, timer)
timer.end_timer()

compound_paths, colors, width, height = solved
Expand All @@ -122,7 +131,7 @@ def index():
timer.end_timer()

timer.start_timer('Bounds Creation')
bounds = compound_path_list_bounds(compound_paths)
bounds = compound_path_list_bounds(compound_paths, tolerance)
timer.end_timer()

app.logger.info(timer.timelog())
Expand Down
12 changes: 6 additions & 6 deletions vectorizing/geometry/bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ def compute_total_bounds(bounds_list):
)

# Calculates bounds of a path made out CubicBeziers and SegmentLists
def path_bounds(path):
bounds = [item.bounds() for item in path]
def path_bounds(path, tolerance):
bounds = [item.bounds(tolerance) for item in path]
return compute_total_bounds(bounds)

# Calculates bounds of a compound path, meaning a list of paths
def compound_path_bounds(compound_path):
bounds = [path_bounds(path) for path in compound_path]
def compound_path_bounds(compound_path, tolerance):
bounds = [path_bounds(path, tolerance) for path in compound_path]
return compute_total_bounds(bounds)

# Calculates bounds of a list of compound paths
def compound_path_list_bounds(compound_path_list):
def compound_path_list_bounds(compound_path_list, tolerance):
bounds = [
compound_path_bounds(compound_path)
compound_path_bounds(compound_path, tolerance)
for compound_path in compound_path_list
if len(compound_path) > 0
]
Expand Down
12 changes: 6 additions & 6 deletions vectorizing/geometry/cubic_bezier.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ def subdivide(points):
)

@njit
def flatten(points):
def flatten(points, tolerance):
stack = [points]
flattened = []

while len(stack):
first = stack.pop()

if is_flat_enough(first):
if is_flat_enough(first, tolerance):
flattened.append(first[0])
flattened.append(first[1])

Expand All @@ -113,9 +113,9 @@ def scaled(self, s):
self.p3 * s
)

def flattened(self):
def flattened(self, tolerance):
points = (tuple(self.p0), tuple(self.p1), tuple(self.p2), tuple(self.p3))
return SegmentList(np.array(flatten(points)))
return SegmentList(np.array(flatten(points, tolerance)))

def bounds(self):
return self.flattened().bounds()
def bounds(self, tolerance):
return self.flattened(tolerance).bounds(tolerance)
7 changes: 5 additions & 2 deletions vectorizing/geometry/potrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ def unfold_polygon(folded_polygon):
# Given a compound path, it converts it to a compound polygon
# by flattening curves.
# All coordinates can be scaled for convenience (see pyclipper)
def compound_path_to_compound_polygon(compound_path, scale = 1):
polygons = [[item.flattened().scaled(scale) for item in path] for path in compound_path]
def compound_path_to_compound_polygon(compound_path, tolerance, scale=1):
polygons = [
[item.flattened(tolerance).scaled(scale) for item in path]
for path in compound_path
]
return [unfold_polygon(folded_polygon) for folded_polygon in polygons]

# Given a compound polygon, it converts it to a compound path.
Expand Down
4 changes: 2 additions & 2 deletions vectorizing/geometry/segment_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ def __init__(self, points):
def scaled(self, s):
return SegmentList(self.points * s)

def flattened(self):
def flattened(self, _):
return self

def to_list(self):
return list(self.points)

def bounds(self):
def bounds(self, _):
t = self.points.T
x = t[0]
y = t[1]
Expand Down
5 changes: 3 additions & 2 deletions vectorizing/solvers/color/ColorSolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
from vectorizing.solvers.color.bitmaps import create_bitmaps

class ColorSolver:
def __init__(self, img, color_count, timer):
def __init__(self, img, color_count, tolerance, timer):
color_count = color_count or ColorSolver.DEFAULT_COLOR_COUNT
color_count = max(color_count, ColorSolver.MIN_COLOR_COUNT)
color_count = min(color_count, ColorSolver.MAX_COLOR_COUNT)
self.color_count = color_count
self.tolerance = tolerance

self.img = limit_size(img)

Expand All @@ -34,7 +35,7 @@ def solve(self):
self.timer.end_timer()

self.timer.start_timer('Polygon Clipping')
compound_paths = remove_layering(traced_bitmaps)
compound_paths = remove_layering(traced_bitmaps, self.tolerance)
self.timer.end_timer()

return [
Expand Down
4 changes: 2 additions & 2 deletions vectorizing/solvers/color/clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

# NOTE: The clipper library uses integer coordinates only for numerical robustness.
# That's why coordinates are scaled by a big factor, to preserve precision.
def remove_layering(traced_bitmaps):
def remove_layering(traced_bitmaps, tolerance):
compound_paths = [
potrace_path_to_compound_path(traced)
for traced in traced_bitmaps
]

compound_polygons = [
compound_path_to_compound_polygon(compound_path, SCALE)
compound_path_to_compound_polygon(compound_path, tolerance, SCALE)
for compound_path in compound_paths
]

Expand Down
Loading