Skip to content

Commit

Permalink
docs: elaborate on the transforms section
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Look committed Mar 10, 2024
1 parent bd87893 commit 58ee546
Show file tree
Hide file tree
Showing 26 changed files with 694 additions and 114,085 deletions.
390 changes: 13 additions & 377 deletions nbs/01_embeddings.ipynb

Large diffs are not rendered by default.

83 changes: 25 additions & 58 deletions nbs/10_svg.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"metadata": {},
"outputs": [],
"source": [
"#| default_exp svg"
"#| default_exp svg_files"
]
},
{
Expand Down Expand Up @@ -60,6 +60,7 @@
"## Converting to Strokes\n",
"\n",
"Steps:\n",
"\n",
"1. **Discretize the SVG:** Convert all paths (including bezier curves, etc) to a set of discrete points.\n",
"2. **Apply SVG Global Transforms:** If the SVG contains something like `<g transform=\"(scale:0.5,-0.4)\">`, apply that to the coordinates.\n",
"3. **Rescale:** Find the min/max/range on the x and y axes. Subtract min, divide by range to get coords between 0 and 1. Multiply by `target_size` to get coordinates in a comprehensible range."
Expand All @@ -71,12 +72,13 @@
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"#| exports\n",
"def svg_to_strokes(input_fname, target_size=200, total_n=1000, min_n=3):\n",
" # convert all paths to a series of points\n",
" all_strokes = discretize_svg(input_fname, total_n=total_n, min_n=min_n)\n",
"\n",
" # apply any global SVG transform instructions\n",
" globally_rescaled_strokes = global_svg_transform(all_strokes, input_fname)\n",
"\n",
" # rescaled to the target image size\n",
" rescaled_strokes = rescale_strokes(globally_rescaled_strokes, target_size)\n",
" return rescaled_strokes"
]
Expand All @@ -88,21 +90,6 @@
"## Discretizing SVG Paths"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def discretize_svg(input_fname, **kwargs):\n",
" paths, attributes, svg_attributes = svgpathtools.svg2paths(\n",
" input_fname, return_svg_attributes=True\n",
" )\n",
" all_strokes = discretize_paths(paths, **kwargs)\n",
" return all_strokes"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -131,11 +118,17 @@
" # drop paths shorter than the minimum number of points.\n",
" if sub_n < min_n:\n",
" continue\n",
" # TODO: consider joining subpaths if the next start point is within 'dist' of this endpoint\n",
" subpaths.append(np.array(to_points(subpath, n=sub_n)))\n",
" return np.array(subpaths, dtype=object)\n",
"\n",
"\n",
" return np.array(subpaths, dtype=object)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| exports\n",
"def discretize_paths(paths, total_n=1000, min_n=3):\n",
" all_strokes = []\n",
" total_length = sum([p.length() for p in paths])\n",
Expand All @@ -144,12 +137,18 @@
" path_n = int(total_n * p.length() / total_length)\n",
" # if number of points is less than the minimum, drop it.\n",
" if path_n < min_n:\n",
" # print(f\"skipping path - path_n={path_n}, path_length={p.length()}, total_length={total_length}\")\n",
" continue\n",
" # print(f\"discretize path - path_n={path_n}, path_length={p.length()}, total_length={total_length}\")\n",
"\n",
" strokes = discretize_path(p, path_n, min_n=min_n)\n",
" all_strokes.extend(strokes)\n",
" return all_strokes\n",
"\n",
"\n",
"def discretize_svg(input_fname, **kwargs):\n",
" paths, attributes, svg_attributes = svgpathtools.svg2paths(\n",
" input_fname, return_svg_attributes=True\n",
" )\n",
" all_strokes = discretize_paths(paths, **kwargs)\n",
" return all_strokes"
]
},
Expand Down Expand Up @@ -274,34 +273,9 @@
" final = identity_xform()\n",
" for x in reversed(xforms):\n",
" final = final.dot(x)\n",
" return final\n",
"\n",
"\n",
"# #| export\n",
"# def apply_path_attribute_transforms(paths, attributes):\n",
"# \"\"\"\n",
"# Find and apply transform attributes specified inline on the <path> elements.\n",
"# (not used in final workflow, b/c the way I vectorized SVG's resulted in some skewed transform attributes)\n",
"# \"\"\"\n",
"# all_coords = []\n",
"# for path, attrs in zip(paths, attributes):\n",
"# path_transform = identity_xform()\n",
"# path_transform_str = attrs.get(\"transform\", None)\n",
"# if path_transform_str:\n",
"# print(f\"path_transform_st={path_transform_str}\")\n",
"# path_transform = build_transforms(path_transform_str)\n",
"# coords = apply_transform(coords, path_transform)\n",
"# all_coords.append(coords)\n",
"# return all_coords"
" return final"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -313,13 +287,6 @@
"\n",
"nbdev.nbdev_export()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 58ee546

Please sign in to comment.