-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawingcnc.py
executable file
·70 lines (55 loc) · 1.89 KB
/
drawingcnc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""
This software is the main script of the DrawingCNC project.
It takes a picture from the camera, process it and returns an EPS file ready to
be used with the CNC milling machine.
Depends on the subprogram take_calibrated_picture, imagemagick and potrace
"""
import subprocess
import sys
def getCalibratedJPEG():
"""
Get a calibrated black and white image from the camera, ready to be
processed by potrace.
Returns the image as raw JPEG data.
"""
try:
calibrated = subprocess.Popen(
"take_calibrated_picture/take_calibrated_picture",
stdout=subprocess.PIPE)
output = calibrated.communicate()[0]
except FileNotFoundError:
sys.exit("Please build the take_calibrated_picture script first.")
return output
def jpegToEPS(jpg_data):
"""
Convert a jpeg image to an eps one, using potrace.
Params:
- jpeg_data is the raw jpeg data to process.
Returns the raw processed EPS data.
"""
convert = subprocess.Popen("convert -:jpg pnm:-",
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
potrace = subprocess.Popen("potrace",
stdin=convert.stdout,
stdout=subprocess.PIPE)
convert.stdout.close()
output = potrace.communicate()[0]
convert.wait()
return output
def getEPSDrawing():
"""
Get the drawing as EPS plans, from the camera.
Returns the raw eps data.
"""
jpeg_data = getCalibratedJPEG()
eps_data = jpegToEPS(jpeg_data)
return eps_data
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.exit("Usage: " + sys.argv[0] + " OUTPUT_FILENAME.eps.")
# Fetch the EPS data and write them to file
eps_data = getEPSDrawing()
with open(sys.argv[1], "w", encoding="utf-8") as fh:
fh.write(eps_data)