Example: Modify layer structure#
This example covers reading of a LayeredFile (A Photoshop file) after which we modify the layer structure by moving layers, removing layers etc. The PhotoshopAPI gives you full control over the layer hierarchy.
Relevant documentation links:
psapi.GroupLayer_8bitpsapi.LayeredFile_8bit
/*
Example of loading a PhotoshopFile, reordering the layer structure and saving it back out. This uses the simplified signature to access the data,
if you instead want to use the "extended" signature which gives lower level access to the PhotoshopFile please look at the ExtendedSignature example.
*/
#include "PhotoshopAPI.h"
#include <unordered_map>
#include <vector>
int main()
{
using namespace NAMESPACE_PSAPI;
// In this case we already know the bit depth but otherwise one could use the PhotoshopFile.m_Header.m_Depth
// variable on the PhotoshopFile to figure it out programmatically. This would need to be done using the
// "extended" read signature.
LayeredFile<bpp8_t> layeredFile = LayeredFile<bpp8_t>::read("LayeredFile.psd");
// The Structure of the Photoshop file we just opened is
// 'Group'
// 'NestedGroup'
// 'NestedImageLayer'
// 'ImageLayer'
//
// and we now want to move the 'NestedGroup' and all of its children to the scene root
// as well as delete 'ImageLayer'. It is advised to do the restructuring in steps
// as shown below to avoid trying to access a layer which no longer exists
// By not specifying a second parameter to move_layer() we tell the function to move it to the scene root
// we could however also move it under another group by passing that group as a second parameter
layeredFile.move_layer("Group/NestedGroup");
layeredFile.remove_layer("Group/ImageLayer");
// One could also export as *.psb and PhotoshopAPI would take care of all the conversions internally
LayeredFile<bpp8_t>::write(std::move(layeredFile), "RearrangedFile.psd");
}
# Example of creating a simple document with a single layer using the PhotoshopAPI.
import os
import numpy as np
import photoshopapi as psapi
def main() -> None:
# In the python bindings we expose a wrapper which reads a LayeredFile instance with the
# correct bit-depth
layered_file = psapi.LayeredFile.read(os.path.join(os.path.dirname(__file__), "LayeredFile.psd"))
# We can now perform any operations we want to, here we move the NestedGroup to the root of the scene by not specifying
# a second parameter and then remove the ImageLayer (this does not remove the Group!)
layered_file.move_layer("Group/NestedGroup")
layered_file.remove_layer("Group/ImageLayer")
# We can also use objects to perform these operations. Note how the indexing reflects
# what we did in the last operation
nested_img_layer = layered_file["NestedGroup"]["NestedImageLayer"]
group_layer = layered_file["Group"]
# We now want to move the NestedImageLayer back under Group
layered_file.move_layer(nested_img_layer, group_layer)
# Note the implicit conversion to PSB works smoothly without any additional work
layered_file.write(os.path.join(os.path.dirname(__file__), "RearrangedFile.psb"))
if __name__ == "__main__":
main()