Example: Create groups#

This example covers creation of a LayeredFile (A Photoshop file) as well as that of a group layer which itself is added to the file and contains child layers.

Relevant documentation links:

/*
Example of creating a layered file with groups through the PhotoshopAPI.
*/

#include "PhotoshopAPI.h"

#include <unordered_map>
#include <vector>


int main()
{
	using namespace NAMESPACE_PSAPI;

	// Initialize some constants that we will need throughout the program
	const static uint32_t width = 64u;
	const static uint32_t height = 64u;

	// Create an 8-bit LayeredFile as our starting point, 8- 16- and 32-bit are fully supported
	LayeredFile<bpp8_t> document = { Enum::ColorMode::RGB, width, height };
	
	GroupLayer<bpp8_t>::Params groupParams = {};
	groupParams.name = "Group";
	// We dont need to specify a width or height if we do not have a mask channel

	// As with image layers we can first add the group to the document root and modify the group after 
	auto groupLayer = std::make_shared<GroupLayer<bpp8_t>>(groupParams);
	document.add_layer(groupLayer);

	// Create an image layer and insert it under the group
	{
		std::unordered_map <Enum::ChannelID, std::vector<bpp8_t>> channelMap;
		channelMap[Enum::ChannelID::Red] = std::vector<bpp8_t>(width * height, 255u);
		channelMap[Enum::ChannelID::Green] = std::vector<bpp8_t>(width * height, 0u);
		channelMap[Enum::ChannelID::Blue] = std::vector<bpp8_t>(width * height, 0u);

		ImageLayer<bpp8_t>::Params layerParams = {};
		layerParams.name = "Layer Red";
		layerParams.width = width;
		layerParams.height = height;

		auto layer = std::make_shared<ImageLayer<bpp8_t>>(
			std::move(channelMap),
			layerParams
		);
		// Adding the layer twice would be invalid and would raise a warning as each layer needs to be created uniquely
		// document.addLayer(layer);
		groupLayer->add_layer(document, layer);
	}

	// Convert to PhotoshopDocument and write to disk. Note that from this point onwards 
	// our LayeredFile instance is no longer usable
	LayeredFile<bpp8_t>::write(std::move(document), "WriteGroupedFile.psd");
}
# Example of creating a document with groups using the PhotoshopAPI.
import os
import numpy as np
import psapi


def main() -> None:
    # Initialize some constants that we will need throughout the program
    width = 64
    height = 64
    color_mode = psapi.enum.ColorMode.rgb

    # Generate our LayeredFile instance
    document = psapi.LayeredFile_8bit(color_mode, width, height)

    # Create an empty group layer, width and height are only relevant if we want to add a mask channel
    group_layer = psapi.GroupLayer_8bit("Group")
    document.add_layer(group_layer)

    img_data = np.zeros((3, height, width), np.uint8)
    img_data[0] = 255

    # When creating an image layer the width and height parameter are required if its not a zero sized layer
    img_layer = psapi.ImageLayer_8bit(img_data, "Layer Red", width=width, height=height)
    # In this example we add the image layer under the group instead which requires us to pass the document as first
    # argument. This is required for runtime checks that a layer isnt added twice
    group_layer.add_layer(document, img_layer)

    document.write(os.path.join(os.path.dirname(__file__), "WriteGroupedFile.psd"))


if __name__ == "__main__":
    main()