-
Notifications
You must be signed in to change notification settings - Fork 0
/
ink_raster2laser.py
271 lines (213 loc) · 11.6 KB
/
ink_raster2laser.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
'''
# Inkscape raster to gcode
# Heiko Schroeter, 12/2020
# Tested with 15W China Laser, grbl 1.1, arduino nano
# 1) Create 8-Bit BW PNG picture file
# 2) Import into Inkscape
# 3) Extension -> Inkraster2Gcode
# 4) Set params
# 5) Create PNG preview and gcode file
#
# Needs: png.py
#
# Operation;
# The orig PNG file is transformed by inkscape to wanted size and then
# read back by png.py into workking matrix.
# Matrix X-row is laser scan- and burnline
# The gcode file uses a general speed set and varies the burn intensity
# by {S} param. This is needed because wen using M4 and M5 the movement
# is shattering due the acceleration and decelration from burn px to burn px.
#
#
# ----------------------------------------------------------------------------
# Copyright (C) 2014 305engineering <[email protected]>
# Original concept by 305engineering.
#
# "THE MODIFIED BEER-WARE LICENSE" (Revision: my own :P):
# <[email protected]> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff (except sell). If we meet some day,
# and you think this stuff is worth it, you can buy me a beer in return.
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ----------------------------------------------------------------------------
'''
import sys
import os
import re
sys.path.append('/usr/share/inkscape/extensions')
sys.path.append('/Applications/Inkscape.app/Contents/Resources/extensions')
import subprocess
import math
import inkex
import png
import array
class GcodeExport(inkex.Effect):
######## Invoked by _main()
def __init__(self):
"""init the effect library and get options from gui"""
inkex.Effect.__init__(self)
# Image export options
self.OptionParser.add_option("-d", "--directory",action="store", type="string", dest="directory", default="/home/",help="Directory for files") ####check_dir
self.OptionParser.add_option("-f", "--filename", action="store", type="string", dest="filename", default="-1.0", help="Export File name")
self.OptionParser.add_option("","--add-numeric-suffix-to-filename", action="store", type="inkbool", dest="add_numeric_suffix_to_filename", default=True,help="Add numeric suffix to filename")
self.OptionParser.add_option("","--linewidth",action="store", type="float", dest="linewidth", default="0.2",help="")
self.OptionParser.add_option("","--picwidth",action="store", type="float", dest="picwidth", default="25.0",help="Pic width in [mm]")
self.OptionParser.add_option("","--picheigth",action="store", type="float", dest="picheigth", default="25.0",help="Pic heigth in [mm]")
# Modal Options for the China 15W Laser module
self.OptionParser.add_option("","--Laser_Max",action="store", type="int", dest="Laser_Max", default="320",help="")
self.OptionParser.add_option("","--Laser_Min",action="store", type="int", dest="Laser_Min", default="25",help="")
#Black Velocity and Moving
self.OptionParser.add_option("","--speed_ON",action="store", type="int", dest="speed_ON", default="1500",help="")
# Commands
self.OptionParser.add_option("","--laseron", action="store", type="string", dest="laseron", default="M04", help="Dynamic Laser ON")
self.OptionParser.add_option("","--laseroff", action="store", type="string", dest="laseroff", default="M05", help="Laser Off")
######## Invoked by __init __ ()
######## Here everything is done
def effect(self):
current_file = self.args[-1]
# inkex.errormsg("FILE: " + current_file)
bg_color = "#ffffff"
suffix = "_Gray_8bit"
##Implementare check_dir
if (os.path.isdir(self.options.directory)) == True:
##CODE THAT IS THE DIRECTORY
#inkex.errormsg("OK") #DEBUG
#Generate file paths to use
pos_file_png_exported = os.path.join(self.options.directory,self.options.filename + suffix + ".png")
pos_file_gcode = os.path.join(self.options.directory,self.options.filename + suffix + ".ngc")
#Export the image to PNG
self.exportPage(pos_file_png_exported,current_file,bg_color)
#Manipulate the PNG image to generate the Gcode file
self.PNGtoGcode(pos_file_png_exported,pos_file_gcode)
else:
inkex.errormsg("Directory does not exist! Please specify existing directory!")
######## EXPORT IMAGE IN PNG
######## Invoked by effect ()
def exportPage(self,pos_file_png_exported,current_file,bg_color):
######## CREATING THE FILE PNG ########
# Needs to be Inkscape.png otherwise png.py wont read the PNG file (wrong signature error)
width = self.options.picwidth/self.options.linewidth
heigth = self.options.picheigth/self.options.linewidth
# Export Image to PNG file to be read back by png.py
command="inkscape -C -e \"%s\" -w %s -h %s -b\"%s\" %s" % (pos_file_png_exported,width,heigth,bg_color,current_file) #Command from command line to export to PNG
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return_code = p.wait()
f = p.stdout
err = p.stderr
def PNGtoGcode(self,pos_file_png_exported,pos_file_gcode):
# Read the exported PNG file by png.py
reader = png.Reader(pos_file_png_exported)
w, h, pixels, metadata = reader.read_flat()
# set gcode Laser Max and Min values
LaserMax = int(self.options.Laser_Max)
LaserMin = int(self.options.Laser_Min)
matrice = [[255 for i in range(w)]for j in range(h)] #List al posto di un array
#Just copy. Pic is ready to use.
for y in range(h): # Y ranges from 0 to h-1
for x in range(w): # X varies from 0 to w-1
pixel_position = (x + y * w)*4 if metadata['alpha'] else (x + y * w)*3
matrice[y][x] = int(pixels[pixel_position])
####Ora matrice_BN contiene l'immagine in Bianco (255) e Nero (0)
B=255
N=0
matrice_BN = [[255 for i in range(w)]for j in range(h)]
matrice_BN = matrice
matrice_BN.reverse() # mirror matrix for proper image display
#### GENERO IL FILE GCODE ####
Laser_ON = False
F_G01 = self.options.speed_ON # main speed
linewidth = self.options.linewidth # laser width or px width
width = self.options.picwidth # X in mm
heigth = self.options.picheigth # Y in mm
Scala = 1.0/linewidth # px/mm
DPIx = int(round(w / width * 25.4)) # DPI in X
DPIy = int(round(h / heigth * 25.4)) # DPI in Y
Scaler = float(LaserMax)/255 # Laser Max Setting
file_gcode = open(pos_file_gcode, 'w') #Create the file
#Configurazioni iniziali standard Gcode
file_gcode.write('; Generated with:\n; "Raster 2 Laser Gcode generator"\n')
file_gcode.write('; 12/2020 Heiko Schroeter (based on 305 Engineering)\n')
file_gcode.write('; GCODE for GRBL 1.1\n;\n;\n')
file_gcode.write('; Width (wanted): ' + str("%.2f" % float(width)) + ' [mm]\n')
file_gcode.write('; Height (wanted): ' + str("%.2f" % float(heigth)) + ' [mm]\n;\n')
file_gcode.write('; Line Width: ' + str("%.2f" % float(linewidth)) + ' [mm]\n')
file_gcode.write('; Width : ' + str(w) + ' [px]\n')
file_gcode.write('; Height: ' + str(h) + ' [px]\n')
file_gcode.write('; Width (calculated): ' + str("%.2f" % float(float(w)/Scala)) + ' [mm]\n')
file_gcode.write('; Height (calculated): ' + str("%.2f" % float(float(h)/Scala)) + ' [mm]\n;\n')
file_gcode.write('; Scala: ' + str("%.2f" % Scala) + ' [px/mm]\n;\n')
file_gcode.write('; DPIx: ' + str(DPIx) + '\n')
file_gcode.write('; DPIy: ' + str(DPIy) + '\n;\n')
file_gcode.write('G21 ; Set units to millimeters\n')
file_gcode.write('G90 ; Use absolute coordinates\n')
file_gcode.write('S0 ; We turn off Laser by Zero PWM\n')
file_gcode.write(self.options.laseron + '; Laser On\n')
file_gcode.write('F' + str(F_G01) + '; Set Speed\n;\n')
#Creazione del Gcode
#allargo la matrice per lavorare su tutta l'immagine
for y in range(h):
matrice_BN[y].append(B)
w = w+1
# Map 0-255 to MaxLaser-MinLaser Values
# i.e. gray_% = 1 - matrice[][]/255
# gray_gcode = (LaserMax-LaserMin)*gray_% + LaserMin
for y in range(h):
if y % 2 == 0 : # forward scanline
for x in range(w):
if matrice_BN[y][x] != B :
if Laser_ON == False :
#file_gcode.write('; newline forward \n')
file_gcode.write('G00 X' + str("%.2f" % float(float(x)/Scala)) + ' Y' + str("%.2f" % float(float(y)/Scala)) + ' S0 \n')
Laser_ON = True
if Laser_ON == True : #DEVO evitare di uscire dalla matrice
if x == w-1 : #controllo fine riga
file_gcode.write('G1 X' + str("%.2f" % float(float(x)/Scala)) + ' S0 \n')
Laser_ON = False
else:
gray_percent = float(1.0 - float(matrice_BN[y][x])/255.0)
gray_gcode = str(int(round(float((LaserMax-LaserMin)*gray_percent + LaserMin))))
if matrice_BN[y][x+1] == B :
file_gcode.write('G1 X' + str("%.2f" % float(float(x+1)/Scala)) + ' S' + gray_gcode + '\n')
Laser_ON = False
elif matrice_BN[y][x] != matrice_BN[y][x+1] :
file_gcode.write('G1 X' + str("%.2f" % float(float(x+1)/Scala)) + ' S' + gray_gcode +'\n')
else: # backward scanline
for x in reversed(range(w)):
if matrice_BN[y][x] != B :
if Laser_ON == False :
#file_gcode.write('; newline backward \n')
file_gcode.write('G00 X' + str("%.2f" % float(float(x+1)/Scala)) + ' Y' + str("%.2f" % float(float(y)/Scala)) + ' S0 \n')
Laser_ON = True
if Laser_ON == True : #DEVO evitare di uscire dalla matrice
if x == 0 : #controllo fine riga ritorno
file_gcode.write('G1 X' + str("%.2f" % float(float(x)/Scala)) + ' S0 \n')
Laser_ON = False
else:
gray_percent = float(1.0 - float(matrice_BN[y][x])/255.0)
gray_gcode = str(int(round(float((LaserMax-LaserMin)*gray_percent + LaserMin))))
if matrice_BN[y][x-1] == B :
file_gcode.write('G1 X' + str("%.2f" % float(float(x)/Scala)) + ' S' + gray_gcode + '\n')
Laser_ON = False
elif matrice_BN[y][x] != matrice_BN[y][x-1] :
file_gcode.write('G1 X' + str("%.2f" % float(float(x)/Scala)) + ' S' + gray_gcode + '\n')
#Configurazioni finali standard Gcode
file_gcode.write(self.options.laseroff + '; Laser Off\n')
file_gcode.write('G00 X0 Y0; home\n')
file_gcode.close() #Chiudo il file
######## ######## ######## ######## ######## ######## ######## ######## ########
def _main():
e=GcodeExport()
e.affect()
exit()
if __name__=="__main__":
_main()