gcode2image 2.4.4__tar.gz → 2.4.6__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.
|
@@ -2,14 +2,27 @@
|
|
|
2
2
|
"""
|
|
3
3
|
gcode2image: convert gcode to image.
|
|
4
4
|
"""
|
|
5
|
-
__version__ = "2.4.
|
|
5
|
+
__version__ = "2.4.6"
|
|
6
6
|
|
|
7
7
|
import sys
|
|
8
8
|
import re
|
|
9
9
|
import argparse
|
|
10
10
|
from PIL import Image
|
|
11
|
+
from pathlib import Path
|
|
11
12
|
import numpy as np
|
|
12
13
|
|
|
14
|
+
def ext_check(expected_extension, openner):
|
|
15
|
+
def extension(filename):
|
|
16
|
+
suffixes = expected_extension.split(',')
|
|
17
|
+
|
|
18
|
+
for suffix in suffixes:
|
|
19
|
+
if filename.lower().endswith(suffix):
|
|
20
|
+
return openner(filename)
|
|
21
|
+
|
|
22
|
+
print(f"Error: unknown file extension '{Path(filename).suffix}', file extension should be one of {suffixes}, exit ...")
|
|
23
|
+
sys.exit(1)
|
|
24
|
+
return extension
|
|
25
|
+
|
|
13
26
|
# set to 10 pixels per mm
|
|
14
27
|
DEFAULT_RESOLUTION = .1
|
|
15
28
|
|
|
@@ -25,7 +38,7 @@ def gcode2image(args) -> np.array:
|
|
|
25
38
|
# set args
|
|
26
39
|
gcode = args.gcode
|
|
27
40
|
|
|
28
|
-
float_pattern = "[+\-]?[0-9]+(\.[0-9]+)?"
|
|
41
|
+
float_pattern = r"[+\-]?[0-9]+(\.[0-9]+)?"
|
|
29
42
|
|
|
30
43
|
X_pattern = f"X{float_pattern}"
|
|
31
44
|
Y_pattern = f"Y{float_pattern}"
|
|
@@ -248,7 +261,7 @@ def gcode2image(args) -> np.array:
|
|
|
248
261
|
|
|
249
262
|
def get_gcode_info():
|
|
250
263
|
nonlocal gcode
|
|
251
|
-
gc_info = { 'min_X': None, 'min_Y': None, 'max_X': None, 'max_Y': None, 'max_S': None, 'pixelsize': None, 'M': None, '
|
|
264
|
+
gc_info = { 'min_X': None, 'min_Y': None, 'max_X': None, 'max_Y': None, 'max_S': None, 'pixelsize': None, 'M': None, 'max_image_laser_power': None }
|
|
252
265
|
|
|
253
266
|
while True:
|
|
254
267
|
line = gcode.readline()
|
|
@@ -289,7 +302,7 @@ def gcode2image(args) -> np.array:
|
|
|
289
302
|
max_laser_power = re.search(f"^;.*maximum_image_laser_power:? +{float_pattern}", line)
|
|
290
303
|
if max_laser_power:
|
|
291
304
|
max_laser_power = re.search(f"maximum_image_laser_power:? +{float_pattern}", max_laser_power.group()).group()
|
|
292
|
-
gc_info['
|
|
305
|
+
gc_info['max_image_laser_power'] = float(re.search(f"{float_pattern}", max_laser_power).group())
|
|
293
306
|
|
|
294
307
|
return gc_info
|
|
295
308
|
|
|
@@ -301,7 +314,7 @@ def gcode2image(args) -> np.array:
|
|
|
301
314
|
gc_info = get_gcode_info()
|
|
302
315
|
|
|
303
316
|
if gc_info['min_X'] is None or gc_info['min_Y'] is None or gc_info['max_X'] is None or gc_info['max_Y'] is None:
|
|
304
|
-
print(f"Error:
|
|
317
|
+
print(f"Error: cannot detect gcode, wrong file type?, exit ...")
|
|
305
318
|
sys.exit(1)
|
|
306
319
|
|
|
307
320
|
# handle resolution
|
|
@@ -319,18 +332,15 @@ def gcode2image(args) -> np.array:
|
|
|
319
332
|
|
|
320
333
|
# handle pixel intensity
|
|
321
334
|
if args.maxintensity is not None:
|
|
335
|
+
# commandline setting is followed
|
|
322
336
|
max_laser_power = args.maxintensity
|
|
323
|
-
elif gc_info['max_laser_power']:
|
|
324
|
-
max_laser_power = gc_info['max_laser_power']
|
|
325
|
-
print(f"maxintensity is set to {max_laser_power} (found within '{args.gcode.name}')")
|
|
326
337
|
else:
|
|
327
|
-
|
|
328
|
-
if
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
print(f"maxintensity is set to {max_laser_power} (option '--maxintensity' is not supplied or found in '{args.gcode.name}')")
|
|
338
|
+
max_laser_power = DEFAULT_MAX_LASER_POWER
|
|
339
|
+
if gc_info['max_image_laser_power']:
|
|
340
|
+
max_laser_power = gc_info['max_image_laser_power']
|
|
341
|
+
if gc_info['max_S'] and gc_info['max_S'] > max_laser_power:
|
|
342
|
+
max_laser_power = gc_info['max_S']
|
|
343
|
+
print(f"maxintensity is set to {max_laser_power}")
|
|
334
344
|
|
|
335
345
|
# handle missing G1 and M3(4) commands
|
|
336
346
|
M = gc_info['M']
|
|
@@ -448,8 +458,8 @@ def main() -> int:
|
|
|
448
458
|
|
|
449
459
|
# Define command line argument interface
|
|
450
460
|
parser = argparse.ArgumentParser(description='Convert a gcode file to image.')
|
|
451
|
-
parser.add_argument('gcode', type=argparse.FileType('r'), default = sys.stdin, help='name of gcode file to convert')
|
|
452
|
-
parser.add_argument('image', type=argparse.FileType('w'), help='image out')
|
|
461
|
+
parser.add_argument('gcode', type=ext_check('.gc,.gcode', argparse.FileType('r')), default = sys.stdin, help='name of gcode file to convert')
|
|
462
|
+
parser.add_argument('image', type=ext_check('.png,.jpg,.jpeg', argparse.FileType('w')), help='image out')
|
|
453
463
|
parser.add_argument('--resolution', default=pixelsize_default, metavar=f"<default: {DEFAULT_RESOLUTION}>",
|
|
454
464
|
type=float, help="define image resolution by pixel size (mm^2)")
|
|
455
465
|
parser.add_argument('--maxintensity', default=max_laser_power_default, metavar=f"<default: {DEFAULT_MAX_LASER_POWER}>",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|