python-laser-auto-image-layers/main.py

73 lines
1.7 KiB
Python

import os
import base64
# paths
base_path = os.path.dirname(os.path.abspath(__file__))
input_path = base_path + '/inputs/'
output_path = base_path + '/outputs/'
# check if template file exists
if not os.path.exists('./template.lbrn'):
print("Template file not found! Please create a template.lbrn file")
exit()
# read template file
with open('./template.lbrn', 'r') as file:
template = file.read()
# create input folder if not exists
if not os.path.exists(input_path):
os.makedirs(input_path)
print("Created inputs folder. Insert images in this folder and run the script again.")
exit()
# read input files
input_files = os.listdir(input_path)
if len(input_files) == 0:
print("No input files found in " + input_path)
exit()
# delete files in output folder
if not os.path.exists(output_path):
os.makedirs(output_path)
output_files = os.listdir(output_path)
for output_file_name in output_files:
file_path = os.path.join(output_path, output_file_name)
if os.path.isfile(file_path):
os.remove(file_path)
# create output files
for input_file_name in input_files:
file_path = os.path.join(input_path, input_file_name)
if os.path.isfile(file_path):
with open(file_path, 'rb') as file:
file_content = file.read()
t = template.replace('%image%', base64.b64encode(file_content).decode('utf-8'))
t = t.replace('%imagePath%', file_path)
# remove file extension and add .lbrn
output_file_name = os.path.splitext(input_file_name)[0] + '.lbrn'
output_file_path = os.path.join(output_path, output_file_name)
with open(output_file_path, 'w') as file:
file.write(t)
print("Created file: " + output_file_path)
print("Done!")