Avoid Performance Degradation when Drawing over Compressed Images

Avoid Performance Degradation when Drawing over Compressed Images

There are times when you want to perform extremely extensive graphic operations on a compressed image. When Aspose.Imaging has to compress and decompress images on the fly, performance degradation may occur. Drawing over compressed images may also carry a performance penalty.

Solution

To avoid performance degradation, we recommend that you convert the image to an uncompressed, or raw, format before performing graphic operations.

Using File Path

In the example that follows, a Png image is converted to another Png and saved to disk. The image is then loaded back before graphic operations are performed on it. The same technique applies for BMP and GIF files.

import aspose.pycore as aspycore
from aspose.imaging import Image, Graphics
from aspose.imaging.fileformats.png import PngImage
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
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.png")), PngImage) as png_image:
save_options = PngOptions()
png_image.save(os.path.join(data_dir, "result.png"), save_options)
with aspycore.as_of(Image.load(os.path.join(data_dir, "result.png")), PngImage) as png_image:
graphics = Graphics(png_image)
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))

Using a Stream Object

The following code snippet shows you how to Png image is converted to another Png image and saved to disk using MemoryStream.

import aspose.pycore as aspycore
from aspose.imaging import Image, Graphics
from aspose.imaging.fileformats.png import PngImage
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 = os.path.join(data_dir, "result.png")
with open(file, "wb") as stream:
with Image.load(os.path.join(data_dir, "template.png")) as png_image:
save_options = PngOptions()
png_image.save_to_stream_with_options(stream, save_options)
with open(file, "rb") as stream:
with aspycore.as_of(Image.load(stream), PngImage) as png_image:
graphics = Graphics(png_image)
if delete_output:
os.remove(file)