I want to select one or more layers in psd and save it as .png file using aspose Java

I want to select one or more layers in psd and save it as .png file using aspose Java. Please let me know how to do it. Category i could not find aspose.psd for java. So i selected this.

@arun.novadontics there are 2 cases how the layer can be exported.

First case is a simple, when you just need to export Layer Data without masks or effects:

    private static void ExportLayerWithoutEffects(PsdImage image) {
        Layer[] layers = image.getLayers();
        boolean[] layerStates = new boolean[layers.length];
        PngOptions options = new PngOptions();
        options.setColorType(PngColorType.TruecolorWithAlpha);
        for (int i = 0; i < layers.length; i++) {
            layers[i].save("layer" + i.toString() + ".png", options);
        }
    }

If you need to export layer with effects please check the following code:

    private static void ExportLayerWithEffect(PsdImage image, int layerToExportIndex) {
        Layer[] layers = image.getLayers();
        boolean[] layerStates = new boolean[layers.length];

        for (int i = 0; i < layers.length; i++) {
            layerStates[i] = layers[i].isVisible();
            layers[i].setVisible(false);
        }

        layers[layerToExportIndex].setVisible(true);

        PngOptions pngOpt = new PngOptions();
        pngOpt.setColorType(PngColorType.TruecolorWithAlpha);

        image.save("SomePath.png", pngOpt);

        for (int i = 0; i < layers.length; i++) {
            layers[i].setVisible(layerStates[i]);
        }
    }

In this case you need to disable visibility of any layer that you don’t want to export. This is made because some layers can affect layers under.

i think you have provided to code to export each layer. I want to select one or more layers and export. Also just to give a try i tried your sample code and getting below error on calling Save method.

value Save is not a member of com.aspose.psd.fileformats.psd.layers.Layer

@arun.novadontics you can use the complex one sample, to make selection of one or more layers for export(Make visible group of layer you want to export). As this moment Aspose.PSD doesn’t provide API to make selection in the terms of Photoshop.

“value Save is not a member of com.aspose.psd.fileformats.psd.layers.Layer” please use Java method location and use “save” mathod written with lower case.

I am able to select layers and export using the complex sample you mentioned. Thank you very much for this help. In the reply you mentioned Aspose.PSD doesn’t provide api to make selection. But we were able to select layers and export it right. I got confused by this statement. Could you please clarifiy?

@arun.novadontics In the Photoshop, you can for example select some region of image, and then copy this region only from current layer, or from all layers below. The Aspose.PSD team works on the similar functionality but it’s no ready at the moment. At this moment the operation that I mentioned can be made only with additional coding. So, I clarified that we are using different terminology to avoid misunderstanding. Glad to read that you were able to make requested operation. Thank you.

Thanks for the explanation.
Is there any way to select layer by using name. Also when layers has sub layers how to select specific sub layer?

@arun.novadontics

  1. You can use getDisplayName or getName Layer properties.

         for (int i = 0; i < psdImage.getLayers().length; i++) {
             if (psdImage.getLayers()[i].getDisplayName() == "myLayer") 
             {
                 // Do with this layer smth
             }
         }
    
  2. You don’t need any additional code to find sub layers. PsdImage class store Layers in the “flat” mode. So, the layers in the group layers will be on the first level, but you also can use the GroupLayer API Example of using group layers in Aspose.PSD for Java|Documentation
    But if you want to get Layers of embedded smart object layer, then you’ll need firstly get embedded PSD File from the SmartObject Smart Object Update and Export using Aspose.PSD for Java|Documentation for example in this case smart object contains separate PSD Image with layers.

Screenshot 2024-04-10 at 12.10.07 PM.png (19.8 KB)

PLease refer the attached screen shot for the layer levels in my project. Now if i want to select right under hip joints and export, how to do it?

@arun.novadontics could you please share file from the screenshot to this topic. For the quick answer the code will be similar to the following:

        for (int i = 0; i < psdImage.getLayers().length; i++) {
            Layer layer = psdImage.getLayers()[i];
            if (layer.getDisplayName() == "hip joints" && layer.getClass().getSimpleName() == "GroupLayer")
            {
                Layer[] layers = ((LayerGroup)layer).getLayers();
                // Recursively find layer by name in this layers
            }
        }

Please down load the field using below link. https://s3.amazonaws.com/static.novadonticsllc.com/hhImages/male/PMSImagesMale.psd
I used below code to select hip joints ->right layer using below code. But in the exported file i could not see this layers gets selected. Please let me know what is the issue with this code.
for (i ← 0 until layers.length) {
val layer = layers(i)

           if (layer.getDisplayName() contains "hip joints"){
            println("inside hip joints group layer")
            layer.setVisible(true);
            val subLayers = layer.asInstanceOf[LayerGroup].getLayers()
            // Recursively find layer by name in this layers
            println("parsing hip joints group layer start")
            for (j <- 0 until subLayers.length){
                 println(subLayers(j).getDisplayName())
                if (subLayers(j).getDisplayName() == "right" ){
                  println("coming inside right condition")
                  subLayers(j).setVisible(true);
                }
            }  
            println("parsing hip joints group layer end")
          }
        }

@arun.novadontics please try to use instead of

if (subLayers(j).getDisplayName() == "right" )
{
    println("coming inside right condition")
    subLayers(j).setVisible(true);
}

Something like:

if (subLayers(j).getDisplayName().endsWith("right") )
{
    println("coming inside right condition")
    subLayers(j).setVisible(true);
}

Because name of layer group differs in PSD Structure: “Layer group: right” instead of just “right”

Actually using the script i provided i noticed it is coming inside the sublayer condition. But in the exported image i could not see the specific layer selected. Please advice.

@arun.novadontics could you please provide full sample to reproduce it. Did the “endWith” update help? Possible the issue in another place now. I’ve investigated this issue, the fix should work.

PFA file. In this file refer method “mergeLayers”.
TestAsposePSDController.scala.zip (2.2 KB)

@arun.novadontics thank you. I’ll downloaded code. I’ll text you back after investigation.

I’ve noted that In the provided code were not applied fix that was mentioned before:

                  println(subLayers(j).getDisplayName())
                 if (subLayers(j).getDisplayName() == "right" ){          //  <--------------------------------------
                   println("coming inside right condition")
                   subLayers(j).setVisible(true);
                 }
             }  

Please try to replace this row:
if (subLayers(j).getDisplayName() == "right" ){

with

if (subLayers(j).getDisplayName().endsWith("right") )

This solution worked. I was not trying this earlier since i got the message in console provided with in if condition in my previous code. i am not sure how it printed that message. Anyway now it is working fine. Thank you very much for helping me to resolve this.

1 Like