-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathimage_to_gcode.py
More file actions
177 lines (166 loc) · 7.09 KB
/
Copy pathimage_to_gcode.py
File metadata and controls
177 lines (166 loc) · 7.09 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#! /usr/bin/env python
from __future__ import absolute_import, division, print_function, unicode_literals
import os
import sys
import cv2 as cv
import argparse
import termcolor
import ast
import copy
ignore_color = (255, 255, 255) #white
color_threshold = 10 #+- this color to be registered - might be needed due to compression alg
class ImageToGcode():
def __init__(self,
img,
spread,
nozzles,
area,
feedrate,
offsets,
verbose=False):
self.img = cv.imread(img)
self.output = ""
self.outFile = os.path.splitext(os.path.abspath(img))[0] + ".gco"
self.spread = spread
self.nozzles = int(nozzles)
self.increment = spread/nozzles
self.printArea = area
self.feedrate = feedrate
#change colors to sauces here
self.red = (0, 0, 255)
self.orange = (0, 152, 255)
self.white = (255, 255, 255)
self.black =(0, 0, 0)
self.green = (0, 255, 0)
self.offsets = offsets
self.debug_to_terminal()
self.make_gcode()
def make_gcode(self):
self.output = "M106\n" # Start Fan
nozzleFirings = [0 for x in range(0, self.img.shape[1])]
nozzleFirings = [copy.copy(nozzleFirings) for x in range(0, self.nozzles)]
scan = range(0, self.img.shape[0])
scan = reversed(scan)
for y in scan:
for x in range(0, self.img.shape[1]):
color = tuple(self.img[y, x])
if color == ignore_color:
pass
elif color == self.red:
nozzleFirings[0][x] += 1 << y % self.nozzles
elif color == self.orange:
nozzleFirings[1][x] += 1 << y % self.nozzles
elif color == self.green:
nozzleFirings[2][x] += 1 << y % self.nozzles
elif color == self.black:
nozzleFirings[3][x] += 1 << y % self.nozzles
else:
pass
if y % self.nozzles == 0 and y > 0:
for headNumber, headVals in enumerate(nozzleFirings):
for column, firingVal in enumerate(headVals):
if firingVal:
currentOffset = self.offsets[headNumber]
self.output += "G1 X"+str(self.increment*column-currentOffset[0])+" Y"+str(y/12*self.spread-currentOffset[1])+" F"+str(self.feedrate)+"\n"
self.output += "M400\n"
self.output += "M700 P"+str(headNumber)+" S"+str(firingVal)+"\n"
#print(str(nozzleFirings))
nozzleFirings = [0 for x in range(0, self.img.shape[1])]
nozzleFirings = [copy.copy(nozzleFirings) for x in range(0, 4)]
f = open(self.outFile, 'w')
f.write(self.output)
f.close()
#print(self.output)
def debug_to_terminal(self):
print("Rows: "+str(self.img.shape[0]))
print("Cols: "+str(self.img.shape[1]))
print("Spread: "+str(self.spread)+"mm")
print("Nozzles: "+str(self.nozzles))
print("Print Area: "+str(self.printArea)+"mm")
rowStr = ""
for y in range(0, self.img.shape[0]):
rowStr = ""
for x in range(0, self.img.shape[1]):
color = tuple(self.img[y, x])
#print(color)
#print(self.red)
if color == self.red:
rowStr += termcolor.colored(" ", 'red', 'on_red')
if color == self.green:
rowStr += termcolor.colored(" ", 'green', 'on_green')
elif color == self.white:
rowStr += termcolor.colored(" ", 'white', 'on_white')
elif color == self.orange:
rowStr += termcolor.colored(" ", 'yellow', 'on_yellow')
elif color == self.black:
rowStr += " "
else:
print(color)
rowStr += termcolor.colored(" ", 'white', 'on_white')
print(rowStr)
if __name__ == "__main__":
#Setup Command line arguments
parser = argparse.ArgumentParser(prog="image_to_gcode.py",
usage="%(prog)s [options] input...",
description="Convert bitmaps to gcode."
)
parser.add_argument("-o", "--output",
default=sys.stdout,
help="Output file, defaults to stdout"
)
parser.add_argument("-s", "--spread",
default="3.175",
help="Nozzle spread (mm). Default: %(default)s"
)
parser.add_argument("-n", "--nozzles",
default="12",
help="Nozzle count. Default: %(default)s"
)
parser.add_argument("-a", "--area",
default="[200, 200]",
help="Print area in millimeters. Default: %(default)s"
)
parser.add_argument("-f", "--feedrate",
default="1000",
help="Print feedrate. Default: %(default)s"
)
parser.add_argument("input",
help="input file, defaults to stdin"
)
parser.add_argument('--version',
action='version',
version="%(prog)s 0.0.1-dev"
)
parser.add_argument("-r", "--red",
default="[0, 0]",
help="Head offset in millimeters. Default: %(default)s"
)
parser.add_argument("-g", "--green",
default="[30.5, 0.1]",
help="Head offset in millimeters. Default: %(default)s"
)
parser.add_argument("-b", "--blue",
default="[0, 0]",
help="Head offset in millimeters. Default: %(default)s"
)
parser.add_argument("-k", "--black",
default="[0, 0]",
help="Head offset in millimeters. Default: %(default)s"
)
#Always output help by default
if len(sys.argv) == 1:
parser.print_help()
sys.exit(0) # Exit after help display
args = parser.parse_args()
offsets = [ast.literal_eval(args.red),
ast.literal_eval(args.green),
ast.literal_eval(args.blue),
ast.literal_eval(args.black)
]
imageProcessor = ImageToGcode(img=args.input,
spread=float(args.spread),
nozzles=float(args.nozzles),
area=ast.literal_eval(args.area),
feedrate=float(args.feedrate),
offsets=offsets
)