extract_metadata: Copy thumbnails to the "thumbs" directory

Include a relative path the the thumbnail in the metadata.   This commit deprecates base64 image transfers.  A future commit will remove base64 data from the metadata, clients will be expected to retrieve thumbnails as a static file.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2021-03-14 12:48:45 -04:00
parent 3b99307109
commit 05b8c4eaef
1 changed files with 35 additions and 16 deletions

View File

@ -206,6 +206,14 @@ class PrusaSlicer(BaseSlicer):
r"; thumbnail begin[;/\+=\w\s]+?; thumbnail end", self.header_data) r"; thumbnail begin[;/\+=\w\s]+?; thumbnail end", self.header_data)
if not thumb_matches: if not thumb_matches:
return None return None
thumb_dir = os.path.join(os.path.dirname(self.path), "thumbs")
if not os.path.exists(thumb_dir):
try:
os.mkdir(thumb_dir)
except Exception:
log_to_stderr(f"Unable to create thumb dir: {thumb_dir}")
return
thumb_base = os.path.splitext(os.path.basename(self.path))[0]
parsed_matches = [] parsed_matches = []
for match in thumb_matches: for match in thumb_matches:
lines = re.split(r"\r?\n", match.replace('; ', '')) lines = re.split(r"\r?\n", match.replace('; ', ''))
@ -221,9 +229,15 @@ class PrusaSlicer(BaseSlicer):
f"MetadataError: Thumbnail Size Mismatch: " f"MetadataError: Thumbnail Size Mismatch: "
f"detected {info[2]}, actual {len(data)}") f"detected {info[2]}, actual {len(data)}")
continue continue
thumb_name = f"{thumb_base}-{info[0]}x{info[1]}.png"
thumb_path = os.path.join(thumb_dir, thumb_name)
rel_thumb_path = os.path.join("thumbs", thumb_name)
with open(thumb_path, "wb") as f:
f.write(base64.b64decode(data.encode()))
parsed_matches.append({ parsed_matches.append({
'width': info[0], 'height': info[1], 'width': info[0], 'height': info[1],
'size': info[2], 'data': data}) 'size': info[2], 'data': data,
'relative_path': rel_thumb_path})
return parsed_matches return parsed_matches
def parse_first_layer_extr_temp(self): def parse_first_layer_extr_temp(self):
@ -319,33 +333,38 @@ class Cura(PrusaSlicer):
if thumbs is not None: if thumbs is not None:
return thumbs return thumbs
# Check for thumbnails extracted from the ufp # Check for thumbnails extracted from the ufp
thumbName = os.path.splitext( thumb_dir = os.path.join(os.path.dirname(self.path), "thumbs")
os.path.basename(self.path))[0] + ".png" thumb_base = os.path.splitext(os.path.basename(self.path))[0]
thumbPath = os.path.join( thumb_path = os.path.join(thumb_dir, f"{thumb_base}.png")
os.path.dirname(self.path), "thumbs", thumbName) rel_path_full = os.path.join("thumbs", f"{thumb_base}.png")
if not os.path.isfile(thumbPath): rel_path_small = os.path.join("thumbs", f"{thumb_base}-32x32.png")
thumb_path_small = os.path.join(thumb_dir, f"{thumb_base}-32x32.png")
if not os.path.isfile(thumb_path):
return None return None
# read file # read file
thumbs = [] thumbs = []
try: try:
with open(thumbPath, 'rb') as thumbFile: with open(thumb_path, 'rb') as thumb_file:
fbytes = thumbFile.read() fbytes = thumb_file.read()
with Image.open(io.BytesIO(fbytes)) as im: with Image.open(io.BytesIO(fbytes)) as im:
thumbFull = base64.b64encode(fbytes).decode() thumb_full_b64 = base64.b64encode(fbytes).decode()
thumbs.append({ thumbs.append({
'width': im.width, 'height': im.height, 'width': im.width, 'height': im.height,
'size': len(thumbFull), 'data': thumbFull 'size': len(thumb_full_b64), 'data': thumb_full_b64,
'relative_path': rel_path_full
}) })
# Create 32x32 thumbnail # Create 32x32 thumbnail
im.thumbnail((32, 32), Image.ANTIALIAS) im.thumbnail((32, 32), Image.ANTIALIAS)
tmpThumb = io.BytesIO() tmp_thumb = io.BytesIO()
im.save(tmpThumb, format="PNG") im.save(tmp_thumb, format="PNG")
thumbSmall = base64.b64encode( im.save(thumb_path_small, format="PNG")
tmpThumb.getbuffer()).decode() thumb_small_b64 = base64.b64encode(
tmpThumb.close() tmp_thumb.getbuffer()).decode()
tmp_thumb.close()
thumbs.insert(0, { thumbs.insert(0, {
'width': im.width, 'height': im.height, 'width': im.width, 'height': im.height,
'size': len(thumbSmall), 'data': thumbSmall 'size': len(thumb_small_b64), 'data': thumb_small_b64,
'relative_path': rel_path_small
}) })
except Exception as e: except Exception as e:
log_to_stderr(str(e)) log_to_stderr(str(e))