Manipulating PNG Images

Specifying Transparency for PNG Images

One of the advantages of saving images in PNG format is that PNG can have transparent background. Aspose.PSD for Java provides the feature for specifying transparency for the PngImage & RasterImage classes as demonstrated in the below section. Aspose.PSD for Java API can be used to set any color as transparent while creating new PNG images or converting existing images to PNG format. For this purposes, the Aspose.PSD for Java API has exposed TransparentColor property and PngColorType enumeration that can be set to specify any color to be rendered as transparent in the PNG image. The below provided code snippet demonstrates how to convert an existing PSD image to PNG image by using PngImage overloaded constructor and specifying the desired color as transparent.

String dataDir = Utils.getDataDir(SpecifyTransparency.class) + "ModifyingAndConvertingImages/";
try (PsdImage psdImage = (PsdImage) Image.load(dataDir + "sample.psd");
// Initialize PNG image with psd image pixel data.
PsdImage pngImage = new PsdImage(psdImage)) {
// specify the PNG image transparency options and save to file.
pngImage.setTransparentColor(Color.getWhite());
pngImage.setTransparentColor(true);
pngImage.save(dataDir + "Specify_Transparency_result.png");
}

Setting Resolution for PNG Images

Aspose.PSD for Java exposes the ResolutionSetting class which can be used to set the resolution for all image formats including PNG. This article demonstrates the usage of the Aspose.PSD for Java API to set the horizontal & vertical resolution parameters for the PNG image format. The code snippet below loads an existing PSD image and converts it to PNG format also changing the resolution.

String dataDir = Utils.getDataDir(SettingResolution.class) + "ModifyingAndConvertingImages/";
try (PsdImage psdImage = (PsdImage) Image.load(dataDir + "sample.psd")) {
// Create an instance of PngOptions, Set the horizontal & vertical resolutions and Save the result on disc
PngOptions options = new PngOptions();
options.setResolutionSettings(new ResolutionSetting(72, 96));
psdImage.save(dataDir + "SettingResolution_output.png", options);
}

Compressing PNG Files

The Portable Network Graphic (PNG) is a lossless compression format for transmitting a bitmap over networks. When you save an image as a PNG file in any program, you may be asked to choose a compression level in a range from 0 to any max level. Setting this value actually compresses the file size and does not decrease the image quality. This article describes how Aspose.PSD APIs allows you to control the PNG file size. Aspose.PSD APIs can be used to set the Compression Levels for the PNG file format using the PngOptions class that has an int type CompressionLevel property. This property accepts a value from 0 to 9 where 9 is the maximum compression. The below provided code snippet demonstrates how to set the Compression Levels using Aspose.PSD for Java API.

String dataDir = Utils.getDataDir(ApplyFilterMethod.class) + "ModifyingAndConvertingImages/";
try (PsdImage psdImage = (PsdImage) Image.load(dataDir + "sample.psd")) {
// Loop over possible CompressionLevel range
for (int i = 0; i <= 9; i++) {
// Create an instance of PngOptions for each resultant PNG, Set CompressionLevel and Save result on disk
PngOptions options = new PngOptions();
options.setCompressionLevel(i);
psdImage.save(dataDir + i + "_out.png", options);
}
}

Specifying Bit Depth for PNG Images

Bit depth in imaging is the number of bits used to indicate the color of a single pixel in a bitmap image. Like all other bitmap formats, PNG color depth is also represented in bit such as 1-bit (2 colors), 2-bit (4 colors), 4-bit (16 colors) and 8-bit (256 colors). Aspose.PSD for Java API can be used to set bit depth for PNG images using BitDepth property exposed by the PngOptions class. At the moment, the BitDepth property can be set to 1, 2, 4 or 8 bits for grayscale and indexed color types. For all other color types only 8 bits are supported. The below provided code snippet demonstrates how to set the Bit Depth for a PNG image.

String dataDir = Utils.getDataDir(SpecifyBitDepth.class) + "ModifyingAndConvertingImages/";
try (PsdImage psdImage = (PsdImage) Image.load(dataDir + "sample.psd")) {
// Create an instance of PngOptions, Set the desired ColorType, BitDepth according to the specified ColorType and save image
PngOptions options = new PngOptions();
options.setColorType(PngColorType.Grayscale);
options.setBitDepth((byte) 1);
psdImage.save(dataDir + "SpecifyBitDepth_out.png", options);
}

Applying Filter Methods on PNG Images

Aspose.PSD for Java exposes the PngFilterType enumeration  which can be used to set the filter type for PNG image. The below provided code snippet demonstrates how to apply  filter on existing PSD file to PNG image by using PngFilterType.

String dataDir = Utils.getDataDir(ApplyFilterMethod.class) + "ModifyingAndConvertingImages/";
try (PsdImage psdImage = (PsdImage) Image.load(dataDir + "sample.psd")) {
// Create an instance of PngOptions, Set the PNG filter method and Save changes to the disc
PngOptions options = new PngOptions();
options.setFilterType(PngFilterType.Paeth);
psdImage.save(dataDir + "ApplyFilterMethod_out.png", options);
}

Changing Background Color of a Transparent PNG Image

PNG format images can have transparent background. Aspose.PSD for Java provides the feature to change the background color of a PNG image that has transparent background. Aspose.PSD for Java API can be used to set/change color of a transparent PNG image. The below provided code snippet demonstrates how to set/change the background color of a transparent PNG image.

String dataDir = Utils.getDataDir(ChangeBackgroundColor.class) + "ModifyingAndConvertingImages/";
try (PsdImage psdImage = (PsdImage) Image.load(dataDir + "sample.psd");
// Convert to PngImage based on PsdImage.
PsdImage pngImage = new PsdImage(psdImage)) {
int[] pixels = pngImage.loadArgb32Pixels(pngImage.getBounds());
// Iterate through the pixel array and Check the pixel information
//that if it is a transparent color pixel and Change the pixel color to white
int transparent = pngImage.getTransparentColor().toArgb();
int replacementColor = Color.getYellow().toArgb();
for (int i = 0; i < pixels.length; i++) {
if (pixels[i] == transparent) {
pixels[i] = replacementColor;
}
}
// Replace the pixel array into the image.
pngImage.saveArgb32Pixels(pngImage.getBounds(), pixels);
pngImage.save(dataDir + "ChangeBackground_out.png");
}