Manipulating JPEG2000 images

Memory Strategy optimization

Loading and creating of JPEG2000 images can be proceeded using memory strategy optimization - i.e. limiting memory buffer size for operation.

from aspose.imaging import Image, LoadOptions
from aspose.imaging.fileformats.jpeg2000 import Jpeg2000Codec
from aspose.imaging.imageoptions import Jpeg2000Options
from aspose.imaging.sources import FileCreateSource
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
# Setting a memory limit of 100 megabytes for target loaded image
# JP2 codec
load_options = LoadOptions()
load_options.buffer_size_hint = 100
with Image.load(os.path.join(data_dir, "template.jp2"), load_options) as image:
image.save(os.path.join(data_dir, "result.jp2"))
if delete_output:
os.remove(os.path.join(data_dir, "result.jp2"))
# Setting a memory limit of 10 megabytes for target created image
# JP2 codec
with Jpeg2000Options() as create_options:
create_options.codec = Jpeg2000Codec.JP2
create_options.buffer_size_hint = 10
create_options.source = FileCreateSource(os.path.join(data_dir, "result-2.jp2"), False)
with Image.create(create_options, 100, 100) as image:
image.save()
if delete_output:
os.remove(os.path.join(data_dir, "result-2.jp2"))
# J2K codec
with Jpeg2000Options() as create_options:
create_options.codec = Jpeg2000Codec.J2K
create_options.buffer_size_hint = 10
create_options.source = FileCreateSource(os.path.join(data_dir, "result.j2k"), False)
with Image.create(create_options, 100, 100) as image:
image.save()
if delete_output:
os.remove(os.path.join(data_dir, "result.j2k"))