Set or Save Transparent Image in Python
Contents
[
Hide
]
Save transparent image with transparency
Issue : Image transparency loss.
Tips : It is necessary to use ColorMode TrueColorWithAlpha to save png with transparency.
Example of conversion transparent png to transparent png
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
import aspose.pycore as aspycore | |
from aspose.imaging import Image, RasterImage | |
from aspose.imaging.fileformats.png import PngColorType | |
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 | |
input_file = os.path.join(templates_folder, "template.png") | |
output_file = os.path.join(templates_folder, "output.png") | |
with aspycore.as_of(Image.load(input_file), RasterImage) as image: | |
obj_init = PngOptions() | |
obj_init.color_type = PngColorType.TRUECOLOR_WITH_ALPHA | |
image.save(output_file, obj_init) | |
if delete_output: | |
os.remove(output_file) |
Example of conversion transparent tiff to transparent png
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
import aspose.pycore as aspycore | |
from aspose.imaging import Image, RasterImage | |
from aspose.imaging.fileformats.png import PngColorType | |
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 | |
input_file = os.path.join(templates_folder, "template.tiff") | |
output_file = os.path.join(templates_folder, "output.png") | |
with aspycore.as_of(Image.load(input_file), RasterImage) as image: | |
obj_init = PngOptions() | |
obj_init.color_type = PngColorType.TRUECOLOR_WITH_ALPHA | |
image.save(output_file, obj_init) | |
if delete_output: | |
os.remove(output_file) |
Example of conversion transparent gif to transparent png
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
import aspose.pycore as aspycore | |
from aspose.imaging import Image, RasterImage | |
from aspose.imaging.fileformats.png import PngColorType | |
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 | |
input_file = os.path.join(templates_folder, "template.gif") | |
output_file = os.path.join(templates_folder, "output.png") | |
with aspycore.as_of(Image.load(input_file), RasterImage) as image: | |
obj_init = PngOptions() | |
obj_init.color_type = PngColorType.TRUECOLOR_WITH_ALPHA | |
image.save(output_file, obj_init) | |
if delete_output: | |
os.remove(output_file) |
Example of conversion transparent webp to transparent png
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
import aspose.pycore as aspycore | |
from aspose.imaging import Image, RasterImage | |
from aspose.imaging.fileformats.png import PngColorType | |
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 | |
input_file = os.path.join(templates_folder, "template.webp") | |
output_file = os.path.join(templates_folder, "output.png") | |
with aspycore.as_of(Image.load(input_file), RasterImage) as image: | |
obj_init = PngOptions() | |
obj_init.color_type = PngColorType.TRUECOLOR_WITH_ALPHA | |
image.save(output_file, obj_init) | |
if delete_output: | |
os.remove(output_file) |