Use Python for PNG Images Adjustment
Create Python Apps to Adjust PNG Images and Photos via Server APIs
How to Adjust PNG Images and Photos with Python
While capturing images, mistakes can occur due to incorrect camera settings. Uncontrolled lighting can also impact the results, and even professional photographs may exhibit imperfections. However, in such scenarios, there is a method to enhance the image by utilizing software tools provided by the Python library. You have the ability to fine-tune the image’s brightness, contrast, and color balance. For instance, if a photo appears overly dim, increasing the brightness will illuminate darker regions, unveiling details that were formerly obscured in shadows. If your image exhibits undesirable color casts due to artificial lighting, you can rectify this using the color gamma adjustment feature. To execute these alterations on PNG files, you can make use of Aspose.Imaging for Python via .NET API which is a feature-rich, powerful and easy to use image manipulation and conversion API for Python platform. You may install it using the following command from your system command.
The system command line
>> pip install aspose-imaging-python-net
Steps to Adjust PNG via Python
You need the aspose-imaging-python-net to try the following workflow in your own environment.
- load PNG files with Image.Load method;
- adjust image;
- save adjusted image to disc in the supported by Aspose.Imaging format.
System Requirements
Aspose.Imaging for Python is supported on all major operating systems. Just make sure that you have the following prerequisites.
- Microsoft Windows / Linux with .NET Core Runtime.
- Python and PyPi package manager.
Adjust PNG images - Python
from aspose.imaging import * | |
from aspose.imaging.fileformats.bmp import * | |
from aspose.imaging.fileformats.dicom import * | |
from aspose.imaging.fileformats.emf import * | |
from aspose.imaging.fileformats.jpeg import * | |
from aspose.imaging.fileformats.jpeg2000 import * | |
from aspose.imaging.fileformats.png import * | |
from aspose.imaging.fileformats.psd import * | |
from aspose.imaging.fileformats.tiff.enums import * | |
from aspose.imaging.imagefilters.filteroptions import * | |
from aspose.imaging.imageoptions import * | |
from aspose.imaging.masking import * | |
from aspose.imaging.masking.options import * | |
from aspose.imaging.masking.result import * | |
from aspose.imaging.sources import * | |
from aspose.pycore import as_of, is_assignable | |
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 adjust_gamma_rgb(): | |
# https://apireference.aspose.com/imaging/python-net/aspose.imaging.rasterimage/adjustgamma | |
filter_images(lambda image: image.adjust_gamma(5, 0.1, 0.1), "adjustgammargb") | |
def adjust_gamma(): | |
filter_images(lambda image: image.adjust_gamma(3), "adjustgamma") | |
def adjust_contrast(): | |
filter_images(lambda image: image.adjust_contrast(50), "adjustcontrast") | |
def adjust_brightness(): | |
filter_images(lambda image: image.adjust_brightness(100), "adjustbrightness") | |
def filter_images(do_filter, filter_name): | |
obj_init = [] | |
obj_init.append("jpg") | |
obj_init.append("png") | |
obj_init.append("bmp") | |
obj_init.append("apng") | |
obj_init.append("dicom") | |
obj_init.append("jp2") | |
obj_init.append("j2k") | |
obj_init.append("tga") | |
obj_init.append("webp") | |
obj_init.append("tif") | |
obj_init.append("gif") | |
obj_init.append("ico") | |
raster_formats = obj_init | |
obj_init2 = [] | |
obj_init2.append("svg") | |
obj_init2.append("otg") | |
obj_init2.append("odg") | |
obj_init2.append("eps") | |
obj_init2.append("wmf") | |
obj_init2.append("emf") | |
obj_init2.append("wmz") | |
obj_init2.append("emz") | |
obj_init2.append("cmx") | |
obj_init2.append("cdr") | |
vector_formats = obj_init2 | |
all_formats = [] | |
all_formats.extend(raster_formats) | |
all_formats.extend(vector_formats) | |
for format_ext in all_formats: | |
input_file = os.path.join(templates_folder, f"template.{format_ext}") | |
if not os.path.exists(input_file): | |
continue | |
is_vector_format = format_ext in vector_formats | |
if is_vector_format: | |
input_file = rasterize_vector_image(format_ext, input_file) | |
output_file = os.path.join(templates_folder, f"{filter_name}_{format_ext}.png") | |
print(format_ext) | |
# explicit type casting from Image to RasterImage | |
with as_of(Image.load(input_file), RasterImage) as image: | |
do_filter(image) | |
multi_page = None | |
# if image implements an IMultipageImage interface | |
if is_assignable(image, IMultipageImage): | |
multi_page = as_of(image, IMultipageImage) | |
if multi_page is not None and multi_page.page_count > 1: | |
# for loop | |
page_index = 0 | |
for page in multi_page.pages: | |
file_name = f"{filter_name}_page{page_index}_{format_ext}.png" | |
page.save(os.path.join(templates_folder + file_name), PngOptions()) | |
# delete an output file | |
delete_file(os.path.join(templates_folder + file_name)) | |
page_index += 1 | |
else: | |
image.save(output_file, PngOptions()) | |
# delete an output file | |
delete_file(output_file) | |
# delete a rasterized file | |
if is_vector_format: | |
delete_file(input_file) | |
def delete_file(file): | |
if delete_output: | |
os.remove(file) | |
def rasterize_vector_image(format_ext, input_file): | |
output_file = os.path.join(templates_folder, f"rasterized.{format_ext}.png") | |
with Image.load(input_file) as image: | |
image.save(output_file, PngOptions()) | |
return output_file | |
# call one of the functions | |
adjust_brightness() | |
#adjust_contrast() | |
#adjust_gamma() | |
#adjust_gamma_rgb() |
About Aspose.Imaging for Python API
Aspose.Imaging API is an image processing solution to create, modify, draw or convert images (photos) within applications. It offers: cross-platform Image processing, including but not limited to conversions between various image formats (including uniform multi-page or multi-frame image processing), modifications such as drawing, working with graphic primitives, transformations (resize, crop, flip&rotate, binarization, grayscale, adjust), advanced image manipulation features (filtering, dithering, masking, deskewing), and memory optimization strategies. It’s a standalone library and does not depend on any software for image operations. One can easily add high-performance image conversion features with native APIs within projects. These are 100% private on-premise APIs and images are processed at your servers.Adjust PNG via Online App
Adjust PNG documents by visiting our Live Demos website The live demo has the following benefits
PNG What is PNG File Format
PNG, Portable Network Graphics, refers to a type of raster image file format that use loseless compression. This file format was created as a replacement of Graphics Interchange Format (GIF) and has no copyright limitations. However, PNG file format does not support animations. PNG file format supports loseless image compression that makes it popular among its users. With the passage of time, PNG has evolved as one of the mostly used image file format. Almost all Operating Systems have support for opening PNG files. For example, Microsoft Windows viewer has the capability to open PNG files as the OS has by default the support available as part of installation.
Read MoreOther Supported Adjust Formats
Using Python, you can easily Adjust different formats including: