image2gcode 1.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- image2gcode-1.1.0/LICENSE +21 -0
- image2gcode-1.1.0/PKG-INFO +60 -0
- image2gcode-1.1.0/README.md +46 -0
- image2gcode-1.1.0/image2gcode.py +204 -0
- image2gcode-1.1.0/pyproject.toml +25 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Johannes Noordanus
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: image2gcode
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: image2gcode: convert an image to gcode.
|
|
5
|
+
Keywords: engraving,laser,image,laser engraving,PWM,gcode
|
|
6
|
+
Author-email: Johannes Noordanus <mailjohannes.mailnoordanus@gmail.com>
|
|
7
|
+
Requires-Python: >=3.10.6
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Requires-Dist: numpy >= 1.24.3
|
|
11
|
+
Requires-Dist: Pillow >= 9.5.0
|
|
12
|
+
Project-URL: Home, https://github.com/johannesnoordanus/image2gcode
|
|
13
|
+
|
|
14
|
+
# image2gcode
|
|
15
|
+
Convert an image to gcode for GRBL v1.1 compatible diode laser engravers.
|
|
16
|
+
|
|
17
|
+
Diode lasers are fast at switching to different power levels. This makes them ideal to write pixels (with discrete intensity levels) at a relatively fast rate. This program converts image pixels to laser 'pixels' one on one. The laserhead sweeps from left to right and vice versa, with each sweep stepping in the other direction. Images (with or without Alpha channel) are converted to black&white first - laser intensity (burn rate) can be seen as a grayscale - and get a white background. Image pixel intensities are inverted (burnrate is an inverse scale) and translated to gcode commands.
|
|
18
|
+
The translation produces dense gcode: pixels with same intensity are drawn with one gcode command and only coordinates and Gcomands that change are writen.
|
|
19
|
+
Note that option```--validate```makes it possible to validate the gcode (result) file (this is inverse conversion gcode2image).
|
|
20
|
+
|
|
21
|
+
It is important to use images that have a high contrast ratio, because burnlevels have less intensity range.
|
|
22
|
+
|
|
23
|
+
### Install
|
|
24
|
+
Depends on python libraries *numpy*, *PIL* and (inverse function) *gcode2image* (https://github.com/johannesnoordanus/gcode2image/)
|
|
25
|
+
```
|
|
26
|
+
> pip install gcode2image
|
|
27
|
+
> pip install image2gcode
|
|
28
|
+
>
|
|
29
|
+
```
|
|
30
|
+
### Example:
|
|
31
|
+
```
|
|
32
|
+
> image2gcode.py logo.png logo.gc
|
|
33
|
+
```
|
|
34
|
+
### Usage:
|
|
35
|
+
```
|
|
36
|
+
> image2gcode.py -h
|
|
37
|
+
usage: image2gcode.py [-h] [--showimage] [--pixelsize <default:0.1>] [--speed <default:800>] [--maxpower <default:300>] [--offset X-off Y-off] [--validate] [-V] image [gcode]
|
|
38
|
+
|
|
39
|
+
Convert an image to gcode for GRBL v1.1 compatible diode laser engravers.
|
|
40
|
+
|
|
41
|
+
positional arguments:
|
|
42
|
+
image image file to be converted to gcode
|
|
43
|
+
gcode gcode output
|
|
44
|
+
|
|
45
|
+
options:
|
|
46
|
+
-h, --help show this help message and exit
|
|
47
|
+
--showimage show b&w converted image
|
|
48
|
+
--pixelsize <default:0.1>
|
|
49
|
+
pixel size in mm (XY-axis): each image pixel is drawn this size
|
|
50
|
+
--speed <default:800>
|
|
51
|
+
draw speed in mm/min
|
|
52
|
+
--maxpower <default:300>
|
|
53
|
+
maximum laser power while drawing (as a rule of thumb set to 1/3 of the machine maximum)
|
|
54
|
+
--offset X-off Y-off laser drawing starts at offset (default: X10.0 Y10.0)
|
|
55
|
+
--validate validate gcode file, do inverse and show image result
|
|
56
|
+
-V, --version show version number and exit
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# image2gcode
|
|
2
|
+
Convert an image to gcode for GRBL v1.1 compatible diode laser engravers.
|
|
3
|
+
|
|
4
|
+
Diode lasers are fast at switching to different power levels. This makes them ideal to write pixels (with discrete intensity levels) at a relatively fast rate. This program converts image pixels to laser 'pixels' one on one. The laserhead sweeps from left to right and vice versa, with each sweep stepping in the other direction. Images (with or without Alpha channel) are converted to black&white first - laser intensity (burn rate) can be seen as a grayscale - and get a white background. Image pixel intensities are inverted (burnrate is an inverse scale) and translated to gcode commands.
|
|
5
|
+
The translation produces dense gcode: pixels with same intensity are drawn with one gcode command and only coordinates and Gcomands that change are writen.
|
|
6
|
+
Note that option```--validate```makes it possible to validate the gcode (result) file (this is inverse conversion gcode2image).
|
|
7
|
+
|
|
8
|
+
It is important to use images that have a high contrast ratio, because burnlevels have less intensity range.
|
|
9
|
+
|
|
10
|
+
### Install
|
|
11
|
+
Depends on python libraries *numpy*, *PIL* and (inverse function) *gcode2image* (https://github.com/johannesnoordanus/gcode2image/)
|
|
12
|
+
```
|
|
13
|
+
> pip install gcode2image
|
|
14
|
+
> pip install image2gcode
|
|
15
|
+
>
|
|
16
|
+
```
|
|
17
|
+
### Example:
|
|
18
|
+
```
|
|
19
|
+
> image2gcode.py logo.png logo.gc
|
|
20
|
+
```
|
|
21
|
+
### Usage:
|
|
22
|
+
```
|
|
23
|
+
> image2gcode.py -h
|
|
24
|
+
usage: image2gcode.py [-h] [--showimage] [--pixelsize <default:0.1>] [--speed <default:800>] [--maxpower <default:300>] [--offset X-off Y-off] [--validate] [-V] image [gcode]
|
|
25
|
+
|
|
26
|
+
Convert an image to gcode for GRBL v1.1 compatible diode laser engravers.
|
|
27
|
+
|
|
28
|
+
positional arguments:
|
|
29
|
+
image image file to be converted to gcode
|
|
30
|
+
gcode gcode output
|
|
31
|
+
|
|
32
|
+
options:
|
|
33
|
+
-h, --help show this help message and exit
|
|
34
|
+
--showimage show b&w converted image
|
|
35
|
+
--pixelsize <default:0.1>
|
|
36
|
+
pixel size in mm (XY-axis): each image pixel is drawn this size
|
|
37
|
+
--speed <default:800>
|
|
38
|
+
draw speed in mm/min
|
|
39
|
+
--maxpower <default:300>
|
|
40
|
+
maximum laser power while drawing (as a rule of thumb set to 1/3 of the machine maximum)
|
|
41
|
+
--offset X-off Y-off laser drawing starts at offset (default: X10.0 Y10.0)
|
|
42
|
+
--validate validate gcode file, do inverse and show image result
|
|
43
|
+
-V, --version show version number and exit
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
image2gcode: convert an image to gcode.
|
|
4
|
+
"""
|
|
5
|
+
__version__ = "1.1.0"
|
|
6
|
+
|
|
7
|
+
import sys
|
|
8
|
+
import argparse
|
|
9
|
+
from argparse import Namespace
|
|
10
|
+
from datetime import datetime
|
|
11
|
+
from PIL import Image
|
|
12
|
+
import numpy as np
|
|
13
|
+
|
|
14
|
+
GCODE2IMAGE = True
|
|
15
|
+
try:
|
|
16
|
+
from gcode2image import gcode2image
|
|
17
|
+
except ImportError:
|
|
18
|
+
GCODE2IMAGE = False
|
|
19
|
+
|
|
20
|
+
def loadImage(imagefile: str = None, showimage: bool = True):
|
|
21
|
+
"""
|
|
22
|
+
loadimage: load an image and convert it to b&w with white background
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
if imagefile:
|
|
26
|
+
# add alpha channel
|
|
27
|
+
img = Image.open(imagefile).convert("RGBA")
|
|
28
|
+
|
|
29
|
+
# create a white background and add it to the image
|
|
30
|
+
img_background = Image.new(mode = "RGBA", size = img.size, color = (255,255,255))
|
|
31
|
+
img = Image.alpha_composite(img_background, img)
|
|
32
|
+
|
|
33
|
+
# convert image to black&white (same size)
|
|
34
|
+
img = img.resize(img.size, Image.Resampling.LANCZOS).convert("L")
|
|
35
|
+
if showimage:
|
|
36
|
+
img.show()
|
|
37
|
+
|
|
38
|
+
# return np array of image
|
|
39
|
+
return np.array(img)
|
|
40
|
+
|
|
41
|
+
return None
|
|
42
|
+
|
|
43
|
+
def image2gcode(img, args) -> str:
|
|
44
|
+
"""
|
|
45
|
+
image2gcode: convert image to gcode
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
# args settings:
|
|
49
|
+
# pixelsize: pixel size in mm (all dimensions)
|
|
50
|
+
# speed: laser head print speed
|
|
51
|
+
# power: maximum power of laser
|
|
52
|
+
|
|
53
|
+
invert_intensity = True # black == white
|
|
54
|
+
|
|
55
|
+
# set X/Y-axis precision to number of digits after
|
|
56
|
+
XY_prec = len(str(args.pixelsize).split('.')[1])
|
|
57
|
+
|
|
58
|
+
# on the safe side: laser stop, fan on, laser on while moving
|
|
59
|
+
gcode_header = ["M5","M8", "M4"]
|
|
60
|
+
# laser off, fan off, program stop
|
|
61
|
+
gcode_footer = ["M5","M9","M2"]
|
|
62
|
+
|
|
63
|
+
# header comment
|
|
64
|
+
gcode = [';\n; ' + sys.argv[0] + " " + __version__ + " (" + str(datetime.now()).split('.')[0] + ")\n" +
|
|
65
|
+
'; Area: ' + str(round(img.shape[1] * args.pixelsize,2)) + "mm x " + str(round(img.shape[0] * args.pixelsize,2)) + "mm (XY)" +
|
|
66
|
+
'; > pixelsize ' + str(args.pixelsize) + ' mm^2, speed ' + str(args.speed) + ' mm/min, maxpower ' + str(args.maxpower) + ', offset ' + str(args.offset) + "\n;\n" ]
|
|
67
|
+
|
|
68
|
+
# init gcode
|
|
69
|
+
gcode += ["G00 G17 G40 G21 G54","G90"]
|
|
70
|
+
# on the safe side: laser stop, fan on, laser on while moving only
|
|
71
|
+
gcode += gcode_header
|
|
72
|
+
|
|
73
|
+
# set printer start coordinates
|
|
74
|
+
X = round(args.offset[0], XY_prec)
|
|
75
|
+
Y = round(args.offset[1], XY_prec)
|
|
76
|
+
|
|
77
|
+
# go to start
|
|
78
|
+
gcode += [f"G0X{X}Y{Y}"]
|
|
79
|
+
|
|
80
|
+
# set write speed and G1 move mode
|
|
81
|
+
# (note that this stays into effect until another G code is executed,
|
|
82
|
+
# so we do not have to repeat this for all coordinates emitted below)
|
|
83
|
+
gcode += [f"G1F{args.speed}"]
|
|
84
|
+
|
|
85
|
+
# Print left to right, right to left (etc.)
|
|
86
|
+
# Optimized gcode:
|
|
87
|
+
# - draw pixels until change of power
|
|
88
|
+
# - emit X/Y coordinates only when they change
|
|
89
|
+
# - emit linear move 'G1' code only once
|
|
90
|
+
# - does not emit in between spaces
|
|
91
|
+
#
|
|
92
|
+
left2right = True
|
|
93
|
+
|
|
94
|
+
# start print
|
|
95
|
+
for line in img:
|
|
96
|
+
|
|
97
|
+
if not left2right:
|
|
98
|
+
# reverse line when printing right to left
|
|
99
|
+
line = np.flip(line)
|
|
100
|
+
|
|
101
|
+
# add line terminator (makes this algorithm regular)
|
|
102
|
+
line = np.append(line,0)
|
|
103
|
+
|
|
104
|
+
# Note that (laser) drawing from a point to a point differs from setting a point (within an image):
|
|
105
|
+
# 0 1 2 3 4 5 6 <-- gcode drawing points
|
|
106
|
+
# one line: |[0]|[1]|[2]|[3]|[4]|[5]| <-- image pixels
|
|
107
|
+
#
|
|
108
|
+
# For example: drawing from X0 to X2 with value S10 corresponds to setting [0] = S10 and [1] = S10
|
|
109
|
+
# Note also that drawing form left to right differs subtly from the reverse
|
|
110
|
+
|
|
111
|
+
# draw this pixel line
|
|
112
|
+
for count, pixel in enumerate(line):
|
|
113
|
+
laserpow = round((1.0 - float(pixel/255)) * args.maxpower) if invert_intensity else round(float(pixel/255) * args.maxpower)
|
|
114
|
+
|
|
115
|
+
# delay emit first pixel (so all same power pixels can be emitted in one sweep)
|
|
116
|
+
if count == 0:
|
|
117
|
+
prev_pow = laserpow
|
|
118
|
+
|
|
119
|
+
# draw pixels until change of power
|
|
120
|
+
if laserpow != prev_pow or count == line.size-1:
|
|
121
|
+
code = f"X{X}"
|
|
122
|
+
code += f"S{prev_pow}"
|
|
123
|
+
gcode += [code]
|
|
124
|
+
if count == line.size-1:
|
|
125
|
+
continue
|
|
126
|
+
prev_pow = laserpow # if laserpow != prev_pow
|
|
127
|
+
|
|
128
|
+
# next point
|
|
129
|
+
X = round(X + (args.pixelsize if left2right else -args.pixelsize), XY_prec)
|
|
130
|
+
|
|
131
|
+
# go to next scan line
|
|
132
|
+
Y = round(Y + args.pixelsize, XY_prec)
|
|
133
|
+
gcode += [f"Y{Y}S0"]
|
|
134
|
+
|
|
135
|
+
# change print direction
|
|
136
|
+
left2right = not left2right
|
|
137
|
+
|
|
138
|
+
# laser off, fan off, program stop
|
|
139
|
+
gcode += gcode_footer
|
|
140
|
+
|
|
141
|
+
return '\n'.join(gcode)
|
|
142
|
+
|
|
143
|
+
def main() -> int:
|
|
144
|
+
"""
|
|
145
|
+
main
|
|
146
|
+
"""
|
|
147
|
+
# defaults
|
|
148
|
+
pixelsize_default = 0.1
|
|
149
|
+
speed_default = 800
|
|
150
|
+
power_default = 300
|
|
151
|
+
|
|
152
|
+
# Define command line argument interface
|
|
153
|
+
parser = argparse.ArgumentParser(description='Convert an image to gcode for GRBL v1.1 compatible diode laser engravers.')
|
|
154
|
+
parser.add_argument('image', type=argparse.FileType('r'), help='image file to be converted to gcode')
|
|
155
|
+
parser.add_argument('gcode', type=argparse.FileType('w'), nargs='?', default = sys.stdout, help='gcode output')
|
|
156
|
+
parser.add_argument('--showimage', action='store_true', default=False, help='show b&w converted image' )
|
|
157
|
+
parser.add_argument('--pixelsize', default=pixelsize_default, metavar="<default:" + str(pixelsize_default)+">",
|
|
158
|
+
type=float, help="pixel size in mm (XY-axis): each image pixel is drawn this size")
|
|
159
|
+
parser.add_argument('--speed', default=speed_default, metavar="<default:" + str(speed_default)+">",
|
|
160
|
+
type=int, help='draw speed in mm/min')
|
|
161
|
+
parser.add_argument('--maxpower', default=power_default, metavar="<default:" +str(power_default)+ ">",
|
|
162
|
+
type=int, help="maximum laser power while drawing (as a rule of thumb set to 1/3 of the machine maximum)")
|
|
163
|
+
parser.add_argument('--offset', default=[10.0, 10.0], nargs=2, metavar=('X-off', 'Y-off'),
|
|
164
|
+
type=float, help="laser drawing starts at offset (default: X10.0 Y10.0)")
|
|
165
|
+
parser.add_argument('--validate', action='store_true', default=False, help='validate gcode file, do inverse and show image result' )
|
|
166
|
+
parser.add_argument('-V', '--version', action='version', version='%(prog)s ' + __version__, help="show version number and exit")
|
|
167
|
+
|
|
168
|
+
args = parser.parse_args()
|
|
169
|
+
|
|
170
|
+
if args.validate and (args.gcode.name == "<stdout>" or not GCODE2IMAGE):
|
|
171
|
+
if not GCODE2IMAGE:
|
|
172
|
+
print("For validation package gcode2image.py must be installed (in $PATH or python install path)")
|
|
173
|
+
else:
|
|
174
|
+
print("For validation a 'gcode filename' argument must be set.")
|
|
175
|
+
parser.print_usage(sys.stderr)
|
|
176
|
+
sys.exit(1)
|
|
177
|
+
|
|
178
|
+
# load and convert image to B&W
|
|
179
|
+
narr = loadImage(args.image.name,args.showimage)
|
|
180
|
+
|
|
181
|
+
print('Area: ' + str(round(narr.shape[1] * args.pixelsize,2)) + "mm x " + str(round(narr.shape[0] * args.pixelsize,2)) + "mm (XY)")
|
|
182
|
+
print('> pixelsize', args.pixelsize, 'mm^2, speed', args.speed, 'mm/min, maxpower ' + str(args.maxpower) + ', offset', args.offset)
|
|
183
|
+
|
|
184
|
+
# emit gcode for image
|
|
185
|
+
print(image2gcode(narr, args), file=args.gcode)
|
|
186
|
+
|
|
187
|
+
if args.validate:
|
|
188
|
+
args.gcode.close()
|
|
189
|
+
with open(args.gcode.name, "r") as fgcode:
|
|
190
|
+
#img = gcode2image(fgcode, narr.shape, args)
|
|
191
|
+
img = gcode2image(Namespace(gcode = fgcode, offset = False, showG0 = True, grid = False))
|
|
192
|
+
|
|
193
|
+
# convert to image
|
|
194
|
+
img = Image.fromarray(img)
|
|
195
|
+
|
|
196
|
+
# show image
|
|
197
|
+
img.show()
|
|
198
|
+
# write image file (png)
|
|
199
|
+
#pic.save(args.gcode.name.split('.')[0] + '_' + sys.argv[0].split('.')[0] + '.png')
|
|
200
|
+
|
|
201
|
+
return 0
|
|
202
|
+
|
|
203
|
+
if __name__ == '__main__':
|
|
204
|
+
sys.exit(main())
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["flit_core >=3.2,<4"]
|
|
3
|
+
build-backend = "flit_core.buildapi"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "image2gcode"
|
|
7
|
+
authors = [{name = "Johannes Noordanus", email = "mailjohannes.mailnoordanus@gmail.com"}]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
license = {file = "LICENSE"}
|
|
10
|
+
classifiers = ["License :: OSI Approved :: MIT License"]
|
|
11
|
+
dynamic = ["version", "description"]
|
|
12
|
+
|
|
13
|
+
keywords = ["engraving", "laser", "image", "laser engraving", "PWM", "gcode"]
|
|
14
|
+
dependencies = [
|
|
15
|
+
"numpy >= 1.24.3",
|
|
16
|
+
"Pillow >= 9.5.0",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
requires-python = ">=3.10.6"
|
|
20
|
+
|
|
21
|
+
[project.scripts]
|
|
22
|
+
image2gcode = "image2gcode:main"
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Home = "https://github.com/johannesnoordanus/image2gcode"
|