Skip to content

Commit

Permalink
Added hatched and show commands, plus various improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
abey79 committed Nov 8, 2019
1 parent 973352e commit b6c5f3b
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 9 deletions.
29 changes: 29 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# TODO

## Generator

- (MVP) load svg
- primitives: segment, square, circle, polygon
- (MVP) script: python file which returns a mls!!
- 3D with _lines_
- _neonlines_

## Filters

- geometry: mask with rectangle, circle, etc.
- (MVP) matrix: static, dynamic

## Output

- (MVP) png
- (MVP) svg option
- scale to phyisical size
- output format (A4, A3, letter, etc.)
- axidraw api

## hatched

- pitch can be float
- add logging
- add progress bar?
- add CLI to hatched directly? (rather point to vpype)
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[build-system]
requires = ["setuptools", "wheel", "numpy"]

[tool.black]
line-length = 95
target_version = ['py37']
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ click
numpy
shapely[vectorized]
svgwrite
matplotlib
opencv-python

git+https://github.com/abey79/hatched.git#egg=hatched


# dev/test
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
version="0.1.0",
description="Vector pipeline for generative art",
long_description=readme,
long_description_content_type="text/markdown",
author="Antoine Beyeler",
url="https://github.com/abey79/vpype",
license=license,
Expand Down
12 changes: 4 additions & 8 deletions vpype/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
from .vpype import cli, processor, generator

# noinspection PyUnresolvedReferences
from .transforms import *

# noinspection PyUnresolvedReferences
# register all commands
from .generators import *

# noinspection PyUnresolvedReferences
from .hatch import *
from .transforms import *
from .files import *

# noinspection PyUnresolvedReferences
from .frames import *
from .show import *

__all__ = ["cli"]
2 changes: 1 addition & 1 deletion vpype/frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ def frame(mls: MultiLineString, offset: float):
]
)

logging.info(f"adding a frame with coordinates {bounds} (offset {offset}")
logging.info(f"adding a frame with coordinates {bounds} (offset {offset})")
return MultiLineString([ls for ls in mls] + [f])
72 changes: 72 additions & 0 deletions vpype/hatch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import cv2
import hatched


import logging

import click

from .vpype import cli, generator


@cli.command("hatched")
@click.argument("filename", type=click.Path(exists=True))
@click.option(
"-l",
"--levels",
nargs=3,
type=int,
default=(64, 128, 192),
help="Pixel value of the 3 threshold between black, dark, light and white (0-255)",
)
@click.option("-s", "--scale", default=1.0, help="Scale factor to apply to the image")
@click.option(
"-i",
"--interpolation",
default="linear",
type=click.Choice(["linear", "nearest"], case_sensitive=False),
help="Interpolation used for scaling",
)
@click.option("-b", "--blur", default=0, type=int, help="Blur radius to apply to the image")
@click.option("-p", "--pitch", default=5, type=int, help="Densest hatching pitch (pixels)")
@click.option(
"-x", "--invert", is_flag=True, help="Invert the image (and levels) before processing"
)
@click.option("-c", "--circular", is_flag=True, help="Use circular instead of diagonal hatches")
@click.option(
"-d", "--show-plot", is_flag=True, help="Display the contours and resulting pattern"
)
@generator
def hatched_gen(
filename: str,
levels,
scale: float,
interpolation: str,
blur: int,
pitch: int,
invert: bool,
circular: bool,
show_plot: bool,
):
"""
Generate hatched pattern from an image (see `hatched` library)
"""
logging.info(f"generating hatches from {filename}")

interp = cv2.INTER_LINEAR
if interpolation == 'nearest':
interp = cv2.INTER_NEAREST

return hatched.hatch(
file_path=filename,
levels=levels,
image_scale=scale,
interpolation=interp,
blur_radius=blur,
hatch_pitch=pitch,
invert=invert,
circular=circular,
show_plot=show_plot,
h_mirror=False, # this is best handled by vpype
save_svg=False, # this is best handled by vpype
)
32 changes: 32 additions & 0 deletions vpype/show.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import logging

import click
from shapely.geometry import MultiLineString
import matplotlib.pyplot as plt

from .vpype import cli, processor


@cli.command()
@click.option("-a", "--show-axes", is_flag=True, help="display matplotlib axes")
@click.option("-g", "--show-grid", is_flag=True, help="display matplotlib grid (implies -a)")
@processor
def show(mls: MultiLineString, show_axes: bool, show_grid: bool):
"""
Display the geometry using matplotlib
"""
logging.info(f"running matplotlib display")

for ls in mls:
plt.plot(*ls.xy, "-k", lw=0.5)
plt.gca().invert_yaxis()
plt.axis("equal")
if show_axes or show_grid:
plt.axis("on")
else:
plt.axis("off")
if show_grid:
plt.grid("on")
plt.show()

return mls

0 comments on commit b6c5f3b

Please sign in to comment.