Aspose.PSD Add Layer From File

Contents
[ ]

Overview

Adding layers to images is a common task in image editing and manipulation. With Aspose.PSD for Java, you can easily add any file as a layer to your PSD (Photoshop Document) files using a simple and straightforward API.

Aspose.PSD for Java provides a powerful set of features for working with PSD files, including layer editing capabilities. The library supports a wide range of image formats, allowing you to add layers from various file types such as JPEG, PNG, TIFF, and more.

To add an image file as a layer, you can use the ‘add_layer’ method provided by the Aspose.PSD API. This method accepts the path to the image file as input and automatically converts it into a layer that can be added to your PSD file.

Example

Here’s a code sample that demonstrates how to add an image file as a layer using Aspose.PSD for Java:

public class AddFileAsLayer {
public static void main(String[] args) {
String inputFile = "inputFile.png";
String outputFile = "AddFileAsLayer.psd";
try {
// Open file as InputStream
try (InputStream inputStream = Files.newInputStream(Paths.get(inputFile))) {
// Create PSD Layer from InputStream
Layer layer = new Layer(inputStream);
// Create PSD Image with the specified size
try (PsdImage psdImage = new PsdImage(layer.getWidth(), layer.getHeight())) {
// Add Layer to PSD Image
psdImage.setLayers(new Layer[]{layer});
// Get Pixels from File
int[] pixels = layer.loadArgb32Pixels(layer.getBounds());
int pixelsLength = pixels.length;
// Fill the pixels data with some values
for (int i = 0; i < pixelsLength; i++) {
if (i % 5 == 0) {
pixels[i] = 500000;
}
}
// Fast Save of Updated Image Data
layer.saveArgb32Pixels(layer.getBounds(), pixels);
// Save PSD Image
try (OutputStream outputStream = Files.newOutputStream(Paths.get(outputFile))) {
psdImage.save(outputStream);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}