metadata: support more PrusaSlicer thumbnail formats
Singed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
1 parent
df3965e896
commit
fa90202eb9
1 file changed
+48
-35
@@ -11,6 +11,7 @@ import argparse
|
||||
import re
|
||||
import os
|
||||
import sys
|
||||
import io
|
||||
import base64
|
||||
import traceback
|
||||
import tempfile
|
||||
@@ -37,6 +38,10 @@ if TYPE_CHECKING:
|
||||
|
||||
UFP_MODEL_PATH = "/3D/model.gcode"
|
||||
UFP_THUMB_PATH = "/Metadata/thumbnail.png"
|
||||
SUPPORTED_THUMB_FORMATS = ("png", "jpg", "qoi")
|
||||
FMT_CONV_MAP = {
|
||||
"qoi": "png"
|
||||
}
|
||||
|
||||
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
|
||||
logger = logging.getLogger("metadata")
|
||||
@@ -255,13 +260,8 @@ class BaseSlicer(object):
|
||||
return None
|
||||
|
||||
def parse_thumbnails(self) -> Optional[List[Dict[str, Any]]]:
|
||||
for data in [self.header_data, self.footer_data]:
|
||||
thumb_matches: List[str] = re.findall(
|
||||
r"; thumbnail begin[;/\+=\w\s]+?; thumbnail end", data)
|
||||
if thumb_matches:
|
||||
break
|
||||
else:
|
||||
return None
|
||||
parsed_matches: List[Dict[str, Any]] = []
|
||||
has_miniature: bool = False
|
||||
thumb_dir = os.path.join(os.path.dirname(self.path), ".thumbs")
|
||||
if not os.path.exists(thumb_dir):
|
||||
try:
|
||||
@@ -270,34 +270,47 @@ class BaseSlicer(object):
|
||||
logger.info(f"Unable to create thumb dir: {thumb_dir}")
|
||||
return None
|
||||
thumb_base = os.path.splitext(os.path.basename(self.path))[0]
|
||||
parsed_matches: List[Dict[str, Any]] = []
|
||||
has_miniature: bool = False
|
||||
for match in thumb_matches:
|
||||
lines = re.split(r"\r?\n", match.replace('; ', ''))
|
||||
info = regex_find_ints(r"(%D)", lines[0])
|
||||
data = "".join(lines[1:-1])
|
||||
if len(info) != 3:
|
||||
logger.info(
|
||||
f"MetadataError: Error parsing thumbnail"
|
||||
f" header: {lines[0]}")
|
||||
continue
|
||||
if len(data) != info[2]:
|
||||
logger.info(
|
||||
f"MetadataError: Thumbnail Size Mismatch: "
|
||||
f"detected {info[2]}, actual {len(data)}")
|
||||
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({
|
||||
'width': info[0], 'height': info[1],
|
||||
'size': os.path.getsize(thumb_path),
|
||||
'relative_path': rel_thumb_path})
|
||||
if info[0] == 32 and info[1] == 32:
|
||||
has_miniature = True
|
||||
if len(parsed_matches) > 0 and not has_miniature:
|
||||
pattern = r"(thumbnail(?:_[A-Za-z0-9]+)?) begin([;/\+=\w\s]+?); \1 end"
|
||||
for data in [self.header_data, self.footer_data]:
|
||||
for match in re.finditer(pattern, data):
|
||||
ext = match.group(1).partition("_")[2].lower() or "png"
|
||||
if ext not in SUPPORTED_THUMB_FORMATS:
|
||||
logger.info(f"Unsupported thumbnail extension: {ext}")
|
||||
continue
|
||||
lines = re.split(r"\r?\n", match.group(2).replace('; ', ''))
|
||||
info = regex_find_ints(r"(%D)", lines[0])
|
||||
data = "".join(lines[1:-1])
|
||||
if len(info) != 3:
|
||||
logger.info(
|
||||
f"MetadataError: Error parsing thumbnail header: {lines[0]}"
|
||||
)
|
||||
continue
|
||||
if len(data) != info[2]:
|
||||
logger.info(
|
||||
f"MetadataError: Thumbnail Size Mismatch: "
|
||||
f"detected {info[2]}, actual {len(data)}")
|
||||
continue
|
||||
dest_ext = FMT_CONV_MAP.get(ext, ext)
|
||||
thumb_name = f"{thumb_base}-{info[0]}x{info[1]}.{dest_ext}"
|
||||
thumb_path = os.path.join(thumb_dir, thumb_name)
|
||||
rel_thumb_path = os.path.join(".thumbs", thumb_name)
|
||||
if dest_ext != ext:
|
||||
# Convert image. Format is determined by destination file
|
||||
# extension. Only formats supported by Pillow should be used.
|
||||
with Image.open(io.BytesIO(base64.b64decode(data.encode()))) as im:
|
||||
im.save(thumb_path)
|
||||
else:
|
||||
with open(thumb_path, "wb") as f:
|
||||
f.write(base64.b64decode(data.encode()))
|
||||
parsed_matches.append({
|
||||
'width': info[0], 'height': info[1],
|
||||
'size': os.path.getsize(thumb_path),
|
||||
'relative_path': rel_thumb_path})
|
||||
if info[0] == 32 and info[1] == 32:
|
||||
has_miniature = True
|
||||
if not parsed_matches:
|
||||
return None
|
||||
if not has_miniature:
|
||||
# find the largest thumb index
|
||||
largest_match = parsed_matches[0]
|
||||
for item in parsed_matches:
|
||||
|
||||
Reference in new issue
Block a user