Reduce file size in png
Contents
[
Hide
]
Reduce file size in png
Issue : Reduce file size in png.
Tips : To reduce file size in png there can be used compression level property and setting of palette.
Example :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.imaging.fileformats.png import PngColorType | |
from aspose.imaging.imageoptions import PngOptions | |
from aspose.imaging import ColorPaletteHelper, Image | |
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 | |
def compress_png(): | |
input_file = os.path.join(templates_folder, "template.png") | |
output_file = os.path.join(templates_folder, "compressed_png.png") | |
with Image.load(input_file) as image: | |
obj_init = PngOptions() | |
obj_init.compression_level = 9 | |
obj_init.progressive = True | |
obj_init.color_type = PngColorType.INDEXED_COLOR | |
obj_init.palette = ColorPaletteHelper.get_close_image_palette(image, 1 << 5) | |
image.save(output_file, obj_init) | |
if delete_output: | |
os.remove(output_file) | |
# run | |
compress_png() |