@@ -129,7 +129,7 @@ class ExtractedBytes:
129129 images : dict [str , bytes ]
130130
131131
132- def _potential_lines (path : str ) -> str :
132+ def _potential_lines_from_gcode_file (path : str ) -> str :
133133 if not os .path .exists (path ):
134134 return ""
135135
@@ -151,20 +151,40 @@ def _potential_lines(path: str) -> str:
151151 return "" .join (content_lines ).replace ("\r \n " , "\n " ).replace (";\n ;\n " , ";\n \n ;\n " )
152152
153153
154- def extract_thumbnails_from_gcode (gcode_path : str ) -> Optional [ExtractedImages ]:
154+ def _to_thumbnail_bytes (extracted : ExtractedImages , format = "PNG" ) -> ExtractedBytes :
155+ data = {}
156+ for image in extracted .images :
157+ sizehint = _image_to_sizehint (image )
158+ if sizehint in data :
159+ continue
160+ data [sizehint ] = _image_to_bytes (image , format = format )
161+ return ExtractedBytes (extractor = extracted .extractor , images = data )
162+
163+
164+ def extract_thumbnails_from_gcode_file (gcode_path : str ) -> Optional [ExtractedImages ]:
155165 logger = logging .getLogger (__name__ )
156166
167+ if not os .path .exists (gcode_path ):
168+ logger .warning (f"Gcode file { gcode_path } doesn't exist" )
169+ return None
170+
157171 logger .info (f"Extracting thumbnails from { gcode_path } ..." )
158172
159- potential_lines = _potential_lines (gcode_path )
173+ potential_lines = _potential_lines_from_gcode_file (gcode_path )
160174 logger .debug (
161175 f"Searching for matches in:\n { _prefix_lines (potential_lines , prefix = ' | ' )} "
162176 )
163177
178+ return extract_thumbnails_from_gcode (potential_lines )
179+
180+
181+ def extract_thumbnails_from_gcode (gcode : str ) -> Optional [ExtractedImages ]:
182+ logger = logging .getLogger (__name__ )
183+
164184 extractors = [
165185 ("generic" , (REGEX_GENERIC , _extract_generic_base64_thumbnails )),
166186 ("snapmaker" , (REGEX_SNAPMAKER , _extract_generic_base64_thumbnails )),
167- ("mks" , (REGEX_MKS , _extract_mks_thumbnails )),
187+ # ("mks", (REGEX_MKS, _extract_mks_thumbnails)),
168188 ("weedo" , (REGEX_WEEDO , _extract_weedo_thumbnails )),
169189 ("qidi" , (REGEX_QIDI , _extract_qidi_thumbnails )),
170190 ("flashprint" , _extract_flashprint_thumbnails ),
@@ -176,45 +196,39 @@ def extract_thumbnails_from_gcode(gcode_path: str) -> Optional[ExtractedImages]:
176196 # regex based extractor
177197 regex , extractor = tooling
178198
179- matches = list (regex .finditer (potential_lines ))
199+ matches = list (regex .finditer (gcode ))
180200 if matches :
181201 logger .debug (f"Detected { name } thumbnails, extracting..." )
182202 return ExtractedImages (extractor = name , images = extractor (matches ))
183203
184204 elif callable (tooling ):
185205 # custom extractor function
186- thumbnails = tooling (gcode_path , potential_lines )
206+ thumbnails = tooling (gcode )
187207 if thumbnails :
188208 return ExtractedImages (extractor = name , images = thumbnails )
189209
190- # none of the regex based extractors matched, could this be flashprint?
191- with open (gcode_path , "rb" ) as f :
192- f .seek (58 )
193- buffer = f .read (14454 )
194- if buffer [0 ] == 0x42 and buffer [1 ] == 0x4D : # BMP magic numbers
195- logger .debug ("Detected flashprint thumbnails, extracting..." )
196- return ExtractedImages (
197- extractor = "flashprint" , images = _extract_flashprint_thumbnails (buffer )
198- )
199-
200210 # if we reach this point, we could not find any thumbnails
201211 return None
202212
203213
204- def extract_thumbnail_bytes_from_gcode (
214+ def extract_thumbnail_bytes_from_gcode_file (
205215 gcode_path : str , format = "PNG"
206216) -> Optional [ExtractedBytes ]:
207- result = extract_thumbnails_from_gcode (gcode_path )
217+ result = extract_thumbnails_from_gcode_file (gcode_path )
208218 if not result :
209219 return None
210220
211- data = {}
212- for image in result .images :
213- sizehint = _image_to_sizehint (image )
214- if sizehint in data :
215- continue
216- data [sizehint ] = _image_to_bytes (image , format = format )
217- return ExtractedBytes (extractor = result .extractor , images = data )
221+ return _to_thumbnail_bytes (result , format = format )
222+
223+
224+ def extract_thumbnail_bytes_from_gcode (
225+ gcode : str , format = "PNG"
226+ ) -> Optional [ExtractedBytes ]:
227+ result = extract_thumbnails_from_gcode (gcode )
228+ if not result :
229+ return None
230+
231+ return _to_thumbnail_bytes (result , format = format )
218232
219233
220234# ~~ extractors
@@ -245,24 +259,7 @@ def _extract_generic_base64_thumbnails(matches: list[re.Match]) -> list[PILImage
245259 return result
246260
247261
248- def _extract_generic_hex_thumbnails (matches : list [re .Match ]) -> list [PILImage ]:
249- """
250- Extracts thumbnails from hex encoded data
251-
252- Will remove any comment prefixes from lines.
253-
254- Expected match groups:
255- * ``data``: hex encoded data
256- """
257- result = []
258- for match in matches :
259- data = _remove_whitespace (_remove_comment_prefix (match .group ("data" )))
260- image = _image_from_hex (data )
261- result .append (image )
262- return result
263-
264-
265- def _extract_mks_thumbnails (matches : list [re .Match ]) -> list [PILImage ]:
262+ def _extract_old_mks_thumbnails (matches : list [re .Match ]) -> list [PILImage ]:
266263 """Extracts a thumbnail from hex binary data used by MKS printers"""
267264
268265 OPTIONS = {";;gimage" : (200 , 200 ), ";simage" : (100 , 100 )}
@@ -392,11 +389,14 @@ def val2rgb(val: int) -> tuple[int, int, int]:
392389 return result
393390
394391
395- def _extract_flashprint_thumbnails (path : str , _lines : str ) -> list [PILImage ]:
396- with open ( path , "rb" ) as f :
392+ def _extract_flashprint_thumbnails (gcode : str ) -> list [PILImage ]:
393+ with io . BytesIO ( gcode . encode () ) as f :
397394 f .seek (58 )
398395 buffer = f .read (14454 )
399396
397+ if not len (buffer ) == 14454 :
398+ return {}
399+
400400 if not (buffer [0 ] == 0x42 and buffer [1 ] == 0x4D ): # BMP magic numbers
401401 return {}
402402
@@ -534,7 +534,7 @@ def main():
534534 print (f"{ args .path } doesn't exist, exiting!" , file = sys .stderr )
535535 sys .exit (1 )
536536
537- result = extract_thumbnail_bytes_from_gcode (args .path , format = "PNG" )
537+ result = extract_thumbnail_bytes_from_gcode_file (args .path , format = "PNG" )
538538 if result :
539539 output_folder = args .output
540540 if not output_folder :
@@ -559,7 +559,7 @@ def main():
559559 print (f"{ args .path } doesn't exist, exiting!" , file = sys .stderr )
560560 sys .exit (1 )
561561
562- result = extract_thumbnails_from_gcode (args .path )
562+ result = extract_thumbnails_from_gcode_file (args .path )
563563 if result :
564564 print (
565565 f'Found { len (result .images )} thumbnails in { args .path } , in format "{ result .extractor } ":'
0 commit comments