Skip to content

Instantly share code, notes, and snippets.

@jacobian
Created July 22, 2019 14:39
Show Gist options
  • Save jacobian/2be66b0a193b908534096a7f73c1e93e to your computer and use it in GitHub Desktop.
Save jacobian/2be66b0a193b908534096a7f73c1e93e to your computer and use it in GitHub Desktop.
"""
Take 2 - trying to minimize jump stitches
Stitch a row \ / \ /, then back
"""
import itertools
import pyembroidery as em
from collections import namedtuple
import click
class Stitch(namedtuple("Stitch", "x y")):
"""
Quick utility Stitch object
Access x/y as fields, and also supports -stitch to easily reverse direction
"""
def __neg__(self):
return Stitch(-self.x, -self.y)
@click.command()
@click.argument("destination", type=click.File("wb"))
@click.option("--png", type=click.File("wb"))
@click.option("--size", type=int, default=16)
@click.option("--xmax", type=int, default=6)
@click.option("--ymax", type=int, default=4)
@click.option("--repeats", type=int, default=3)
def stitch(destination, png, size, xmax, ymax, repeats):
pattern = em.EmbPattern()
pattern.add_thread("red")
# The various diagonal stitches
DOWN_RIGHT = Stitch(+size, +size)
UP_RIGHT = Stitch(+size, -size)
UP_LEFT = Stitch(-size, -size)
DOWN_LEFT = Stitch(-size, +size)
# start with a stitch at the origin to get the needle down (see the docs)
pattern.stitch_abs(0, 0)
for y in range(ymax):
# Jump to the beginning of the row - upper left
pattern.stitch_abs(0, y * size)
# stitch \ / \ / forward
forward_stitches = itertools.cycle([DOWN_RIGHT, UP_RIGHT])
for x, stitch in zip(range(xmax), forward_stitches):
pattern.stitch(*stitch)
for i in range(repeats - 1):
stitch = -stitch
pattern.stitch(*stitch)
# tie off & trim end of row (I don't actually know my machine does this but
# let's find out)
pattern.add_stitch_relative(em.TIE_OFF)
pattern.trim()
# jump to end of row and tie on - up right
pattern.add_stitch_absolute(em.TIE_ON, xmax * size, (y + 1) * size)
# stitch backwards, opposite crosses
reverse_stitches = itertools.cycle([UP_LEFT, DOWN_LEFT])
for x, stitch in zip(range(xmax), reverse_stitches):
pattern.stitch(*stitch)
for i in range(repeats - 1):
stitch = -stitch
pattern.stitch(*stitch)
# we finish in the right spot for the next row so no need to tie on/off
# here. but in the future might need to do that as above.
em.write_pes(pattern, destination, settings={"tie_on": True, "tie_off": True})
if png:
em.write_png(pattern, png, settings={"background": "white"})
if __name__ == "__main__":
stitch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment