Skip to content

Commit ae028d7

Browse files
committed
Add basic pystitch examples
1 parent 6d5c65e commit ae028d7

File tree

13 files changed

+171
-0
lines changed

13 files changed

+171
-0
lines changed

.vscode/extensions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"ms-python.python"
4+
]
5+
}

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"files.trimFinalNewlines": true,
3+
"files.trimTrailingWhitespace": true,
4+
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
# Bytes to Bobbins
2+
3+
## Examples
4+
5+
Go to [examples/README.md](examples/README.md) for instructions.

examples/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Outputs
2+
**/out/**
3+
!**/out/.gitkeep

examples/Pipfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
pystitch = "*"
8+
9+
[dev-packages]
10+
11+
[requires]
12+
python_version = "3.12"
13+
python_full_version = "3.12.3"

examples/Pipfile.lock

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Examples
2+
3+
Examples are written in Python. To set up environment, install `pipenv` and run:
4+
```bash
5+
pipenv sync
6+
pipenv shell
7+
```
8+
To run an example, enter one of the directories and execute `main.py`, for example:
9+
```bash
10+
cd python-pystitch-helloworld
11+
python main.py
12+
```
13+
You can preview output files in Inkscape with Ink/Stitch installed.
14+
15+
## Example list
16+
17+
* `python-pystitch-helloworld`: Basic example, a small square
18+
* `python-pystitch-addblock`: Simplified syntax, multiple squares with color changing
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from pathlib import Path
2+
from pystitch import EmbPattern
3+
from typing import List, Tuple
4+
from utils import parse_args, UNITS_PER_MM
5+
6+
args = parse_args(default_filename = f'out/{Path(__file__).parent.name}.jef')
7+
8+
# Create and encode pattern
9+
STITCH_UNITS = 2 * UNITS_PER_MM
10+
CENTERED_SQUARE_COORDS = [(-1/2, -1/2), (1/2, -1/2), (1/2, 1/2), (-1/2, 1/2), (-1/2, -1/2)]
11+
12+
def scale_pattern(coords: List[Tuple[float, float]], scale: float):
13+
return list((scale*x, scale*y) for (x, y) in coords)
14+
15+
pattern = EmbPattern()
16+
pattern.add_block(scale_pattern(CENTERED_SQUARE_COORDS, 1*STITCH_UNITS), "red") # 2 mm stitch
17+
pattern.add_block(scale_pattern(CENTERED_SQUARE_COORDS, 2*STITCH_UNITS), "orange") # 4 mm stitch
18+
pattern.add_block(scale_pattern(CENTERED_SQUARE_COORDS, 3*STITCH_UNITS), "yellow") # 6 mm stitch
19+
pattern.write(args.output)

examples/python-pystitch-addblock/out/.gitkeep

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from argparse import ArgumentParser
2+
import os
3+
4+
UNITS_PER_MM = 10
5+
6+
def parse_args(default_filename: str = 'out/out.jef'):
7+
ALLOWED_EXTENSIONS = [
8+
'.dst', # Tajima Embroidery Format
9+
'.exp', # Melco Expanded Embroidery Format
10+
'.gcode', # gcode Format, Text File
11+
'.jef', # Janome Embroidery Format
12+
'.pec', # Brother Embroidery Format
13+
'.pes', # Brother Embroidery Format
14+
'.tbf', # Tajima Embroidery Format
15+
'.u01', # Barudan Embroidery Format
16+
'.vp3', # Pfaff Embroidery Format
17+
'.xxx' # Singer Embroidery Format
18+
]
19+
20+
parser = ArgumentParser(description="Generate an embroidery design.")
21+
parser.add_argument('-o', '--output', type=str, default=default_filename, help=f"Output file path. {default_filename} by default")
22+
args = parser.parse_args()
23+
24+
_, ext = os.path.splitext(args.output)
25+
if ext.lower() not in ALLOWED_EXTENSIONS:
26+
raise ValueError(f"Invalid file extension '{ext}'. Allowed extensions: {ALLOWED_EXTENSIONS}")
27+
28+
return args

0 commit comments

Comments
 (0)