import aspose.pycore as aspycore
from aspose.imaging import *
from aspose.imaging.fileformats.tiff import *
from aspose.imaging.fileformats.tiff.enums import *
from aspose.imaging.imageoptions import *
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:
		tiff_frame.save(os.path.join(data_dir, f"{i}_out.tiff"), TiffOptions(TiffExpectedFormat.TIFF_JPEG_RGB))
		if delete_output:
			os.remove(os.path.join(data_dir, f"{i}_out.tiff"))
		i += 1