import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.fileformats.tiff import TiffImage
from aspose.imaging.imageoptions import PngOptions
import os

if 'TEMPLATE_DIR' in os.environ:
	templates_folder = os.environ['TEMPLATE_DIR']
else:
	templates_folder = r"C:\Users\USER\Downloads\templates"

delete_output = 'SAVE_OUTPUT' not in os.environ


data_dir = templates_folder
file_name = os.path.join(data_dir, "template.tiff")
# Create an instance of TiffImage and load the file from disc
with aspycore.as_of(Image.load(file_name), TiffImage) as multi_image:
	# Initialize a variable to keep track of the frames in the image, Iterate over the tiff frame collection and Save the image
	i = 0
	for tiff_frame in multi_image.frames:
		out_file_name = os.path.join(data_dir, f"{i}_out.png")
		tiff_frame.save(out_file_name, PngOptions())
		if delete_output:
			os.remove(out_file_name)
		i += 1