Orca Slicer thumbnail generating script

version 2
requires the installation of Pillow.
pip install Pillow
This commit is contained in:
ThomasToka 2023-09-21 18:18:20 +02:00 committed by GitHub
parent 2685809c69
commit f1ef402147
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,9 @@
# ------------------------------------------------------------------------------
import sys
import os
import base64
from PIL import Image
from io import BytesIO
# Get the g-code source file name
sourceFile = sys.argv[1]
@ -16,23 +18,78 @@ sourceFile = sys.argv[1]
with open(sourceFile, "r") as f:
lines = f.readlines()
new_lines = []
remove_lines = False
for line in lines:
# Find the index of the line containing '; HEADER_BLOCK_START'
header_start = None
for i, line in enumerate(lines):
if line.strip() == '; HEADER_BLOCK_START':
remove_lines = True
elif line.strip() == '; THUMBNAIL_BLOCK_START':
remove_lines = False
elif not remove_lines:
new_lines.append(line)
header_start = i
break
try:
with open(sourceFile, "w+") as of:
of.write(''.join(new_lines))
except Exception as e:
print(f'Error writing output file: {str(e)}')
input()
finally:
of.close()
f.close()
# Find the index of the line containing '; THUMBNAIL_BLOCK_START'
thumbnail_start = None
for i, line in enumerate(lines):
if line.strip() == '; THUMBNAIL_BLOCK_START':
thumbnail_start = i
break
# Ensure both start markers were found for header and thumbnail
if header_start is not None and thumbnail_start is not None:
# Remove the lines between '; HEADER_BLOCK_START' and '; THUMBNAIL_BLOCK_START' including the marker lines
del lines[header_start:thumbnail_start+1]
# Find the index of the line containing '; thumbnail begin' or a similar line
index_begin = None
for i, line in enumerate(lines):
if '; thumbnail begin' in line:
index_begin = i
break
# Find the index of the line containing '; thumbnail end'
index_end = None
for i, line in enumerate(lines[index_begin:], start=index_begin):
if line.strip() == '; thumbnail end':
index_end = i
break
# Ensure both start and end markers were found for thumbnail
if index_begin is not None and index_end is not None:
# Extract the original PNG data from the lines between '; thumbnail begin' and '; thumbnail end'
original_png_data = "".join(lines[index_begin + 1:index_end])
# Remove "; " from the extracted PNG data
original_png_data = original_png_data.replace("; ", "")
try:
# Decode the original PNG data
png_data_bytes = base64.b64decode(original_png_data)
image = Image.open(BytesIO(png_data_bytes))
image = image.convert("RGB") # Convert to RGB mode
# Encode the image as JPEG
buffer = BytesIO()
image.save(buffer, format="JPEG")
image_jpg_data = buffer.getvalue()
# Base64 encode the JPEG data
image_jpg_base64 = base64.b64encode(image_jpg_data).decode('utf-8')
# Calculate the size of the JPEG data in bytes
jpg_data_size = len(image_jpg_base64)
# Replace the size in the '; thumbnail begin' line
lines[index_begin] = f'; thumbnail begin 250x250 {jpg_data_size}\n'
# Calculate the maximum line length
max_line_length = 79 - len("; ")
# Prepare the injected JPEG data with each line on a new line
injected_jpg_data = ["; " + image_jpg_base64[i:i + max_line_length] for i in range(0, len(image_jpg_base64), max_line_length)]
# Insert the new formatted JPEG data into the original lines
lines[index_begin + 1:index_end] = [line + "\n" for line in injected_jpg_data]
# Write the modified content back to the original file
with open(sourceFile, "w") as f:
f.writelines(lines)
except Exception as e:
sys.exit(1)