Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active December 16, 2020 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/0761bcf61600e99dae1491621168dc1a to your computer and use it in GitHub Desktop.
Save aspose-com-gists/0761bcf61600e99dae1491621168dc1a to your computer and use it in GitHub Desktop.
Convert PSD to raster images in Java
// Load a PSD file as an image and caste it into PsdImage
PsdImage psdImage = (PsdImage) Image.load("sample.psd");
// Create an instance of PngOptions class
PngOptions pngOptions = new PngOptions();
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
// Loop through the list of layers
for (int i = 0; i < psdImage.getLayers().length; i++) {
// Convert and save the layer to PNG file format.
psdImage.getLayers()[i].save(String.format("layer_out{0}.png", i + 1), pngOptions);
}
// Load an existing PSD image as Image
Image image = Image.load("sample.psd");
// Create an instance of PngOptions class
PngOptions pngOptions = new PngOptions();
// Create an instance of BmpOptions class
BmpOptions bmpOptions = new BmpOptions();
// Create an instance of GifOptions class
GifOptions gifOptions = new GifOptions();
// Create an instance of JpegOptions class
JpegOptions jpegOptions = new JpegOptions();
// Create an instance of Jpeg2000Options class
Jpeg2000Options jpeg2000Options = new Jpeg2000Options();
// Call the save method, provide output path and export options to convert PSD file to various raster file formats.
image.save("psd-to-png.png", pngOptions);
image.save("psd-to-bmp.bmp", bmpOptions);
image.save("psd-to-gif.gif", gifOptions);
image.save("psd-to-jpg.jpeg", jpegOptions);
image.save("psd-to-jp2.jp2", jpeg2000Options);
// Load image
Image img = Image.load("sample.psd");
// Create PDF options
PdfOptions options = new PdfOptions();
// Convert PSD to PDF
img.save("psd-to-pdf.pdf", options );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment