### Install Pytoshop Source: https://context7.com/mdboom/pytoshop/llms.txt Install the library using pip. ```bash pip install pytoshop ``` -------------------------------- ### Read and Write PSD File with Pytoshop Source: https://github.com/mdboom/pytoshop/blob/master/docs/usage.md This Python code snippet demonstrates how to open a PSD file, read its content using pytoshop.read(), and then write the data to a new PSD file using psd.write(). It requires the pytoshop library to be installed. ```python import pytoshop with open('image.psd', 'rb') as fd: psd = pytoshop.read(fd) with open('updated.psd', 'wb') as fd: psd.write(fd) ``` -------------------------------- ### GridAndGuidesInfo Class Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.image_resources.md Represents grid and guides information within image resources. ```APIDOC ### *class* pytoshop.image_resources.GridAndGuidesInfo(name: unicode = '', grid_hori: int = 0, grid_vert: int = 0, guides: List[GuideResourceBlock] = []) Grid and guides resource. #### *property* grid_hori *: int* Document-specific grid (horizontal). In 1/32 pt. #### *property* grid_vert *: int* Document-specific grid (vertical). In 1/32 pt. #### *property* guides *: List[GuideResourceBlock]* Guides. See `GuideResourceBlock`. ``` -------------------------------- ### Initialize and Write PSD Header Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.core.md Demonstrates how to instantiate a Header object with specific image properties and write it to a binary file stream. This is essential for creating the initial structure of a PSD file. ```python from pytoshop.core import Header from pytoshop.enums import ColorMode, ColorDepth header = Header(num_channels=3, height=100, width=100, color_mode=ColorMode.rgb, depth=ColorDepth.depth8) with open('output.psd', 'wb') as f: header.write(f) ``` -------------------------------- ### Create PSD Files from Scratch Source: https://context7.com/mdboom/pytoshop/llms.txt Initialize new PSD or PSB files programmatically using the PsdFile class. ```python from pytoshop import core, enums import numpy as np psd = core.PsdFile( version=enums.Version.version_1, num_channels=3, height=100, width=100, depth=enums.ColorDepth.depth8, color_mode=enums.ColorMode.rgb, compression=enums.Compression.rle ) with open('new_file.psd', 'wb') as fd: psd.write(fd) ``` -------------------------------- ### Initialize and Write BlendingRange Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.blending_range.md Demonstrates how to instantiate a BlendingRange object and write it to a binary file stream. This requires a valid file-like object and a header object. ```python from pytoshop.blending_range import BlendingRange # Initialize with black and white values blend = BlendingRange(black0=0, black1=50, white0=200, white1=255) # Write to a binary file-like object with open('output.bin', 'wb') as f: blend.write(f, header=psd_header) ``` -------------------------------- ### Create and Save PSD Structure Source: https://context7.com/mdboom/pytoshop/llms.txt Demonstrates how to define image layers and groups, assemble them into a PSD file structure, and write the final file to disk. ```python image_layer = Image(name='Red Square', top=10, left=10, channels={0: red_channel, 1: green_channel, 2: blue_channel, -1: alpha_channel}, opacity=255, visible=True, blend_mode=enums.BlendMode.normal, color_mode=enums.ColorMode.rgb) group = Group(name='My Group', visible=True, opacity=255, blend_mode=enums.BlendMode.pass_through, layers=[image_layer], closed=False) psd = nested_layers_to_psd(layers=[group], color_mode=enums.ColorMode.rgb, version=enums.Version.version_1, compression=enums.Compression.rle, depth=enums.ColorDepth.depth8) with open('created.psd', 'wb') as fd: psd.write(fd) ``` -------------------------------- ### Configure Blend Modes and Color Modes Source: https://context7.com/mdboom/pytoshop/llms.txt Provides a reference for available blend modes and color modes, and demonstrates initializing a new PSD file with specific color depth and mode settings. ```python from pytoshop import enums, core psd = core.PsdFile(version=enums.Version.version_1, num_channels=4, height=200, width=200, depth=enums.ColorDepth.depth16, color_mode=enums.ColorMode.cmyk) ``` -------------------------------- ### Reading a PSD file with pytoshop.read Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.read.md This snippet demonstrates how to open a PSD file in binary mode and pass the file descriptor to the pytoshop.read function to obtain a PsdFile object. ```python import pytoshop with open('example.psd', 'rb') as fd: psd = pytoshop.read(fd) print(psd) ``` -------------------------------- ### Configure Image Compression in Pytoshop Source: https://context7.com/mdboom/pytoshop/llms.txt Demonstrates how to set compression types for PSD files and individual layer channels. It utilizes the enums module to select between raw, RLE, and ZIP compression methods. ```python from pytoshop import enums from pytoshop import core from pytoshop.layers import ChannelImageData import numpy as np # Available compression types compression_types = { 'Raw (uncompressed)': enums.Compression.raw, 'RLE (PackBits)': enums.Compression.rle, 'ZIP': enums.Compression.zip, 'ZIP with prediction': enums.Compression.zip_prediction, } # Set compression when creating PSD psd = core.PsdFile( version=enums.Version.version_1, num_channels=3, height=100, width=100, depth=enums.ColorDepth.depth8, color_mode=enums.ColorMode.rgb, compression=enums.Compression.rle ) # Change compression on existing layer channels channel_data = ChannelImageData( image=np.zeros((100, 100), dtype=np.uint8), compression=enums.Compression.zip ) ``` -------------------------------- ### Read GlobalLayerMaskInfo from File (Python) Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.layers.md Instantiates GlobalLayerMaskInfo from a file-like object. Requires a file descriptor and header information. This class represents global layer mask settings, including opacity and kind. ```python GlobalLayerMaskInfo.read(fd=BinaryIO, header=core.Header) -> GlobalLayerMaskInfo ``` -------------------------------- ### pytoshop.read Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.read.md Reads a PSD file from a binary file-like object and returns a PsdFile instance. ```APIDOC ## FUNCTION pytoshop.read ### Description Reads a PSD file from a provided file-like object. The input must be a binary stream that is both readable and seekable. ### Method N/A (Python Function) ### Endpoint pytoshop.read(fd) ### Parameters #### Path Parameters - **fd** (BinaryIO) - Required - A file-like object opened in binary mode that is readable and seekable. ### Request Example ```python import pytoshop with open('example.psd', 'rb') as f: psd = pytoshop.read(f) ``` ### Response #### Success Response - **psdfile** (PsdFile) - An instance of the PsdFile class representing the parsed PSD structure. #### Response Example { "psdfile": "" } ``` -------------------------------- ### Initialize ImageData class Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.image_data.md Demonstrates the instantiation of the ImageData class with various parameters including channels, dimensions, and compression settings. This class is used to store and manage non-layer image data within the pytoshop ecosystem. ```python from pytoshop.image_data import ImageData from pytoshop.enums import Compression # Example initialization of ImageData image_data = ImageData( channels=None, height=1024, width=1024, num_channels=3, depth=8, compression=Compression.raw ) ``` -------------------------------- ### Create PSD with nested layer groups Source: https://context7.com/mdboom/pytoshop/llms.txt Demonstrates how to generate a PSD file from a list of layer objects using the nested_layers_to_psd function. This approach simplifies the creation of complex layer hierarchies by handling the underlying binary format automatically. ```python from pytoshop import nested_layers_to_psd, enums psd = nested_layers_to_psd( layers=[outer_group], color_mode=enums.ColorMode.rgb, compression=enums.Compression.rle ) with open('grouped_layers.psd', 'wb') as fd: psd.write(fd) ``` -------------------------------- ### Read Layer Info (Python) Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.layers.md Instantiates LayerInfo from a file-like object. This method requires a readable, seekable file descriptor opened in binary mode and a header object containing global file information. ```python LayerInfo.read(fd, header) ``` -------------------------------- ### Work with Layer Masks Source: https://context7.com/mdboom/pytoshop/llms.txt Explains how to extract mask properties and access the user layer mask channel data from a PSD file. ```python import pytoshop with open('masked.psd', 'rb') as fd: psd = pytoshop.read(fd) for layer in psd.layer_and_mask_info.layer_info.layer_records: mask = layer.mask if enums.ChannelId.user_layer_mask in layer.channels: mask_data = layer.channels[enums.ChannelId.user_layer_mask] mask_array = mask_data.image ``` -------------------------------- ### Read and Parse PSD File Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.core.md Shows how to read an existing PSD file from a binary stream into a PsdFile object. This allows for accessing various sections like image resources, layer information, and raw image data. ```python from pytoshop.core import PsdFile with open('input.psd', 'rb') as f: psd = PsdFile.read(f) print(f"Image dimensions: {psd.width}x{psd.height}") ``` -------------------------------- ### Compression Options Source: https://context7.com/mdboom/pytoshop/llms.txt Configure image compression when writing PSD files. Supports Raw, RLE, ZIP, and ZIP with prediction. ```APIDOC ## Compression Options Configure image compression when writing PSD files. ### Description This section details how to set the compression method for PSD files using the `pytoshop.enums.Compression` enum. Available options include raw (uncompressed), RLE (PackBits), ZIP, and ZIP with prediction. ### Usage **Setting compression when creating a new PSD file:** ```python from pytoshop import core from pytoshop import enums psd = core.PsdFile( version=enums.Version.version_1, num_channels=3, height=100, width=100, depth=enums.ColorDepth.depth8, color_mode=enums.ColorMode.rgb, compression=enums.Compression.rle # Example: Use RLE compression ) ``` **Setting compression for individual layer channels:** ```python from pytoshop.layers import ChannelImageData import numpy as np from pytoshop import enums channel_data = ChannelImageData( image=np.zeros((100, 100), dtype=np.uint8), compression=enums.Compression.zip # Example: Use ZIP compression ) ``` ### Available Compression Types - **Raw (uncompressed)**: `enums.Compression.raw` - **RLE (PackBits)**: `enums.Compression.rle` (Recommended for most cases) - **ZIP**: `enums.Compression.zip` - **ZIP with prediction**: `enums.Compression.zip_prediction` ``` -------------------------------- ### Read ChannelImageData from File (Python) Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.layers.md Instantiates ChannelImageData from a file-like object. Requires a file descriptor, header information, shape, and size of the image data. It reads a single plane of channel image data. ```python ChannelImageData.read(fd=BinaryIO, header=core.Header, shape=Tuple[int, int], size=int) ``` -------------------------------- ### Write Layer and Mask Info (Python) Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.layers.md Writes layer and mask information to a binary file descriptor. Requires a header object for context. The file descriptor must be writable, seekable, and opened in binary mode. ```python layer_and_mask_info.write(fd, header) ``` -------------------------------- ### Write GlobalLayerMaskInfo to File (Python) Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.layers.md Writes GlobalLayerMaskInfo to a file-like object. Requires a file descriptor and header information. This method serializes the global layer mask settings. ```python GlobalLayerMaskInfo.write(fd=BinaryIO, header=core.Header) -> None ``` -------------------------------- ### Create Nested Layer Groups Source: https://context7.com/mdboom/pytoshop/llms.txt Demonstrates the creation of complex layer structures using the Group and Image classes, allowing for nested organization and specific blend mode settings. ```python import numpy as np from pytoshop import enums from pytoshop.user.nested_layers import Group, Image layer1 = Image(name='Background', top=0, left=0, channels={0: np.full((100, 100), 200, dtype=np.uint8)}, color_mode=enums.ColorMode.rgb) layer2 = Image(name='Foreground Element', top=20, left=20, channels={0: np.full((50, 50), 255, dtype=np.uint8)}, opacity=200, blend_mode=enums.BlendMode.multiply, color_mode=enums.ColorMode.rgb) inner_group = Group(name='Inner Group', layers=[layer2], blend_mode=enums.BlendMode.normal, closed=True) outer_group = Group(name='Outer Group', layers=[layer1, inner_group], blend_mode=enums.BlendMode.pass_through, closed=False) ``` -------------------------------- ### Reading PSD Files Source: https://context7.com/mdboom/pytoshop/llms.txt Explains how to open, parse, and extract metadata or image data from existing PSD/PSB files. ```APIDOC ## READ PSD/PSB File ### Description Parses a PSD or PSB file stream into a PsdFile object to access layers, channels, and metadata. ### Method Function Call: `pytoshop.read(file_object)` ### Parameters - **file_object** (file-like object) - Required - A binary file handle opened in 'rb' mode. ### Response - **psd** (PsdFile) - An object containing file properties (version, dimensions, color mode) and layer information. ### Request Example ```python with open('image.psd', 'rb') as fd: psd = pytoshop.read(fd) ``` ``` -------------------------------- ### Access PSD Image Resources and Metadata Source: https://context7.com/mdboom/pytoshop/llms.txt Explains how to retrieve image resources from a PSD file, such as resolution, ICC profiles, and XMP metadata, using specific resource IDs. ```python import pytoshop from pytoshop import enums with open('image.psd', 'rb') as fd: psd = pytoshop.read(fd) # Access image resources resources = psd.image_resources # Get specific resource blocks by ID layers_group_info = resources.get_block(enums.ImageResourceID.layers_group_info) if layers_group_info: print(f"Layer group IDs: {layers_group_info.group_ids}") ``` -------------------------------- ### Pascal String Handling Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.util.md Utilities for reading and writing Pascal strings. ```APIDOC ## pytoshop.util.read_pascal_string ### Description Read a UTF-8-encoded Pascal string from a file. ### Method N/A (Function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) - **value** (unicode) - The unicode value of the string. #### Response Example N/A ``` -------------------------------- ### String Utility Functions Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.util.md Functions for calculating string lengths and writing various string formats to binary files. ```APIDOC ## FUNCTION pytoshop.util.write_pascal_string ### Description Writes a UTF-8-encoded Pascal string to a file with optional padding. ### Parameters - **fd** (BinaryIO) - Required - File-like object opened in binary write mode. - **value** (str) - Required - The unicode string to write. - **padding** (int) - Optional - Padding bytes to ensure total length is a multiple of this value. ## FUNCTION pytoshop.util.write_unicode_string ### Description Writes a UTF-16-BE-encoded Unicode string (with length prefix) to a file. ### Parameters - **fd** (BinaryIO) - Required - File-like object opened in binary write mode. - **value** (str) - Required - The unicode string to write. ``` -------------------------------- ### Access Layer Channels Source: https://context7.com/mdboom/pytoshop/llms.txt Shows how to iterate through PSD layers and access their individual color channels as NumPy arrays. ```python import pytoshop from pytoshop import enums with open('image.psd', 'rb') as fd: psd = pytoshop.read(fd) for layer in psd.layer_and_mask_info.layer_info.layer_records: for channel_id, channel_data in layer.channels.items(): channel_name = enums.ChannelId(channel_id).name image_array = channel_data.image print(f"Channel {channel_name}: shape={image_array.shape}, dtype={image_array.dtype}") ``` -------------------------------- ### Enumeration Constants Overview Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.enums.md Overview of the core enumeration classes used to define PSD file properties and image processing parameters. ```APIDOC ## Enumerations in pytoshop.enums ### Description This module provides standard enumeration classes to ensure type safety and consistency when defining PSD file attributes such as color modes, compression algorithms, and layer configurations. ### Key Enumerations - **ColorMode**: Defines the color mode of the image. - **Compression**: Specifies the compression algorithm (e.g., raw, rle, zip). - **ColorDepth**: Bits-per-pixel-per-channel (1, 8, 16, 32). - **Version**: PSD file version (1 for PSD, 2 for PSB). - **SectionDividerSetting**: Defines the display state of grouped layers (open, closed, etc.). ### Usage Example ```python from pytoshop.enums import ColorMode, Compression # Example of using an enum mode = ColorMode.rgb compression = Compression.rle ``` ### Color Space Details - **rgb**: Red, Green, Blue (16-bit). - **cmyk**: Cyan, Magenta, Yellow, Black. - **lab**: Lightness, a-chrominance, b-chrominance. ``` -------------------------------- ### Utilities API Source: https://github.com/mdboom/pytoshop/blob/master/docs/api.md Provides miscellaneous utility functions for Pytoshop. ```APIDOC ## Utilities API ### Miscellaneous Utilities Offers various utility functions that are helpful for working with PSD files and the Pytoshop library. ### Method N/A (Module) ### Endpoint N/A ### Parameters None ### Request Example ```python import pytoshop.util # Example usage (specific functions depend on the module's implementation) # utility_result = pytoshop.util.some_utility_function() ``` ### Response N/A ``` -------------------------------- ### Padding Utilities Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.util.md Functions for padding numbers and strings. ```APIDOC ## pytoshop.util.pad ### Description Pads an integer up to the given divisor. ### Method N/A (Function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) - **int** (int) - The padded integer. #### Response Example N/A ``` ```APIDOC ## pytoshop.util.pascal_string_length ### Description Calculates the total length of writing a UTF-8-encoded Pascal string to disk. ### Method N/A (Function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) - **length** (int) - The length, in bytes. #### Response Example N/A ``` -------------------------------- ### Convert PSD to PSB Format Source: https://context7.com/mdboom/pytoshop/llms.txt Shows how to read a PSD file, modify its version property to convert it to PSB (Large Document Format), and save the result. This is useful for handling documents exceeding standard PSD pixel dimensions. ```python import pytoshop import io # Read PSD file with open('original.psd', 'rb') as fd: psd = pytoshop.read(fd) # Convert to PSB format (Large Document) psd.version = 2 # Write as PSB with open('converted.psb', 'wb') as fd: psd.write(fd) # Convert back to PSD psd.version = 1 ``` -------------------------------- ### Class: VersionInfo Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.image_resources.md Handles versioning information for the Photoshop file, including reader/writer details and merged data status. ```APIDOC ## CLASS pytoshop.image_resources.VersionInfo ### Description Stores versioning metadata for the image file. ### Parameters - **name** (unicode) - Optional - Resource name - **version** (int) - Optional - Version number - **has_real_merged_data** (bool) - Optional - Flag for merged data - **writer** (unicode) - Optional - Writer name - **reader** (unicode) - Optional - Reader name - **file_version** (int) - Optional - File version number ### Properties - **version** (int) - Version number - **has_real_merged_data** (bool) - Indicates if real merged data exists - **writer** (unicode) - Name of the writer - **reader** (unicode) - Name of the reader - **file_version** (int) - Specific file version ``` -------------------------------- ### write Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.layers.md Writes the layer data to a file-like object. ```APIDOC ## POST /layer/write ### Description Writes the layer's data to a specified file descriptor, using provided header information. ### Method POST ### Endpoint `/layer/write` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **fd** (BinaryIO) - Required - A file-like object that must be writable, seekable, and opened in binary mode. - **header** (core.Header) - Required - An object containing global file information. ### Request Example ```json { "fd": "", "header": { "example": "Header object details" } } ``` ### Response #### Success Response (200) - **None** - Indicates the operation was successful. #### Response Example ```json null ``` ``` -------------------------------- ### Read Layer and Mask Info (Python) Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.layers.md Reads layer and mask information from a binary file descriptor. Requires a header object for context. The file descriptor must be readable, seekable, and opened in binary mode. ```python LayerAndMaskInfo.read(fd, header) ``` -------------------------------- ### Working with Layer Groups Source: https://context7.com/mdboom/pytoshop/llms.txt Create and manipulate layer group structures within PSD files. ```APIDOC ## Working with Layer Groups Create and manipulate layer group structures. ### Description This section provides guidance on creating and organizing layers into groups, including nested structures. It demonstrates how to define image layers and group them using the `Group` class, specifying properties like name, blend mode, and visibility. ### Usage **Creating image layers:** ```python import numpy as np from pytoshop import enums from pytoshop.user.nested_layers import Group, Image layer1 = Image( name='Background', top=0, left=0, channels={ 0: np.full((100, 100), 200, dtype=np.uint8), # R 1: np.full((100, 100), 200, dtype=np.uint8), # G 2: np.full((100, 100), 200, dtype=np.uint8), # B -1: np.full((100, 100), 255, dtype=np.uint8) # A }, color_mode=enums.ColorMode.rgb ) layer2 = Image( name='Foreground Element', top=20, left=20, channels={ 0: np.full((50, 50), 255, dtype=np.uint8), 1: np.full((50, 50), 0, dtype=np.uint8), 2: np.full((50, 50), 0, dtype=np.uint8), -1: np.full((50, 50), 200, dtype=np.uint8) }, opacity=200, blend_mode=enums.BlendMode.multiply, color_mode=enums.ColorMode.rgb ) ``` **Creating nested group structures:** ```python # Create an inner group inner_group = Group( name='Inner Group', layers=[layer2], blend_mode=enums.BlendMode.normal, closed=True ) # Create an outer group containing the inner group and other layers outer_group = Group( name='Outer Group', layers=[layer1, inner_group], blend_mode=enums.BlendMode.pass_through, closed=False ) # To write this structure to a PSD file, you would typically use: # from pytoshop.user.nested_layers import nested_layers_to_psd # psd_data = nested_layers_to_psd(outer_group) # ... then write psd_data to a file. ``` ### Group Properties - **name**: Name of the group (string). - **layers**: List of layers or nested groups within this group. - **blend_mode**: The blend mode of the group (e.g., `enums.BlendMode.normal`). - **closed**: Boolean indicating if the group is initially collapsed (`True`) or expanded (`False`). ``` -------------------------------- ### LayerMask Methods Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.layers.md This section details the methods available for the LayerMask class, including reading from and writing to file descriptors. ```APIDOC ## LayerMask Methods ### Description Methods for instantiating a LayerMask object from a file and writing it back to a file. ### Methods #### read(fd: BinaryIO, header: core.Header) -> LayerMask Instantiate from a file-like object. * **Parameters:** * **fd** (file-like object) – Must be readable, seekable and open in binary mode. * **header** (PsdFile object) – An object to get global file information from. #### write(fd: BinaryIO, header: core.Header) -> None Write to a file-like object. * **Parameters:** * **fd** (file-like object) – Must be writable, seekable and open in binary mode. * **header** (PsdFile object) – An object to get global file information from. ``` -------------------------------- ### Write Constant Image Data (Raw Compression) Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.codecs.md Writes a virtual image containing a constant value to a raw stream. It requires a file-like object, the constant value, image dimensions (width, rows), color depth, and PSD version. ```python def compress_constant_raw(fd: BinaryIO, value: int, width: int, rows: int, depth: enums.ColorDepth, version: enums.Version) -> None: """Write a virtual image containing a constant to a raw stream.""" pass ``` -------------------------------- ### POST /nested_layers_to_psd Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.user.nested_layers.md Converts a hierarchy of nested Layer instances into a PsdFile object. ```APIDOC ## POST /nested_layers_to_psd ### Description Converts a hierarchy of nested Layer instances to a PsdFile instance with configurable color modes, compression, and dimensions. ### Method POST ### Endpoint pytoshop.user.nested_layers.nested_layers_to_psd ### Parameters #### Request Body - **layers** (List[Layer]) - Required - The hierarchy of layers to create. - **color_mode** (int) - Required - The color mode of the resulting PSD file. - **version** (int) - Optional - The version of the PSD spec to follow. - **compression** (int) - Optional - The method of image compression (default: RLE). - **depth** (int) - Optional - The color depth of the resulting image. - **size** (Tuple[int, int]) - Optional - The shape (height, width) of the PSD file. - **vector_mask** (bool) - Optional - Whether to use a vector rectangle mask. ### Request Example { "layers": [...], "color_mode": 1, "version": 1, "compression": 0 } ### Response #### Success Response (200) - **psdfile** (PsdFile) - The resulting PSD file object. #### Response Example { "psdfile": "" } ``` -------------------------------- ### User API Source: https://github.com/mdboom/pytoshop/blob/master/docs/api.md Provides functions for interacting with user-related aspects of PSD files. ```APIDOC ## User API ### Read PSD File Reads a PSD file from a file-like object. ### Method `read` ### Endpoint N/A (Function call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import pytoshop with open("example.psd", "rb") as fd: psd = pytoshop.read(fd) ``` ### Response #### Success Response (200) - **psd** (object) - The parsed PSD file object. #### Response Example ```python # Example PSD object structure (simplified) { "header": {...}, "color_mode_data": {...}, "image_data": {...}, "image_resources": {...}, "layers": [...] } ``` ``` ```APIDOC ## User API - Nested Layers ### Convert PSD to/from Nested Layers Converts a PSD file to or from a nested layers structure. ### Method `user.nested_layers` ### Endpoint N/A (Function call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import pytoshop.user # Assuming 'psd' is a loaded PsdFile object # nested_layers_data = pytoshop.user.nested_layers(psd) ``` ### Response #### Success Response (200) - **nested_layers_data** (object) - Data representing the PSD file in a nested layers format. #### Response Example ```json { "layers": [ { "name": "Background", "visible": true, "opacity": 1.0, "children": [] }, { "name": "Group 1", "visible": true, "opacity": 1.0, "children": [ { "name": "Layer 1", "visible": true, "opacity": 1.0, "children": [] } ] } ] } ``` ``` -------------------------------- ### Write and Modify PSD Files Source: https://context7.com/mdboom/pytoshop/llms.txt Modify existing PSD files or write them to disk/memory buffers. ```python import pytoshop import io with open('original.psd', 'rb') as fd: psd = pytoshop.read(fd) for layer in psd.layer_and_mask_info.layer_info.layer_records: layer.opacity = 200 layer.visible = True with open('modified.psd', 'wb') as fd: psd.write(fd) ``` -------------------------------- ### ImageResourceBlock Class Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.image_resources.md Provides methods for reading and writing individual image resource blocks. ```APIDOC ## CLASS pytoshop.image_resources.ImageResourceBlock ### Description Represents a single image resource block within a PSD file. ### Methods #### classmethod read(fd: BinaryIO, header: [core.Header](pytoshop.core.md#pytoshop.core.Header)) -> [ImageResourceBlock](#pytoshop.image_resources.ImageResourceBlock) Instantiate from a file-like object. * **Parameters:** * **fd** (*file-like object*) – Must be readable, seekable and open in binary mode. * **header** (*PsdFile object*) – An object to get global file information from. #### write(fd: BinaryIO, header: [core.Header](pytoshop.core.md#pytoshop.core.Header)) -> [None](https://docs.python.org/3/library/constants.html#None) Write to a file-like object. * **Parameters:** * **fd** (*file-like object*) – Must be writable, seekable and open in binary mode. * **header** (*PsdFile object*) – An object to get global file information from. ### Properties #### resource_id: [int](https://docs.python.org/3/library/functions.html#int) Type of image resource. ``` -------------------------------- ### Write Layer Info (Python) Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.layers.md Writes LayerInfo data to a file-like object. This method requires a writable, seekable file descriptor opened in binary mode and a header object containing global file information. ```python layer_info.write(fd, header) ``` -------------------------------- ### Manage BlendingRangePair and BlendingRanges Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.blending_range.md Shows how to group BlendingRange objects into pairs and manage the collection of blending ranges for a PSD layer. This is used to define composite and channel-specific blending behavior. ```python from pytoshop.blending_range import BlendingRange, BlendingRangePair, BlendingRanges # Create a pair range_pair = BlendingRangePair(src=BlendingRange(0, 50, 200, 255), dst=BlendingRange(0, 255, 0, 255)) # Manage all blending ranges all_ranges = BlendingRanges(composite_gray_blend=range_pair, channels=[]) # Calculate section length length = all_ranges.length(header=psd_header) ``` -------------------------------- ### ImageResources Overview Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.image_resources.md Provides an overview of the ImageResources section and its purpose in storing non-pixel data. ```APIDOC ## ImageResources Overview The [`ImageResources`](#pytoshop.image_resources.ImageResources) section. Image resource blocks are the basic building unit of several file formats, including Photoshop’s native file format, JPEG, and TIFF. Image resources are used to store non-pixel data associated with images, such as pen tool paths. ``` -------------------------------- ### File Reading Utilities Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.util.md Utilities for reading various data types from file-like objects. ```APIDOC ## pytoshop.util.read_unicode_string ### Description Read a UTF-16-BE-encoded Unicode string (with length) from a file. ### Method N/A (Function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) - **value** (str) - The unicode value of the string. #### Response Example N/A ``` ```APIDOC ## pytoshop.util.read_value ### Description Read a values from a file-like object. ### Method N/A (Function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) - **value** (any) - The value(s) read from the file. If a single value, it is returned alone. If multiple values, a tuple is returned. #### Response Example N/A ``` -------------------------------- ### Compress Image Data (Python) Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.codecs.md Writes image data to a file-like object using a specified compression type. It supports compressing actual numpy arrays or generating virtual images from a constant scalar value. Input validation is performed on image data shape, channels, and depth against provided parameters. ```python def compress_image(fd: BinaryIO, image: ndarray, compression: int, shape: Tuple[int, int], num_channels: int, depth: int, version: int) -> None: """Write an image with the given compression type. * **Parameters:** * **fd** (*file-like object*) – Writable file-like object, open in binary mode. * **image** (*2-D numpy array* *or* *scalar*) – The image to compress. Must be unsigned integer with 8, 16 or 32 bits. If *depth* is 1, the array should have dtype `uint8` with a byte per pixel. If a scalar, a “virtual” image will be used as if the image contained only that constant value. * **compression** ([*enums.Compression*](pytoshop.enums.md#pytoshop.enums.Compression)) – The compression format to use. See [`enums.Compression`](pytoshop.enums.md#pytoshop.enums.Compression). * **shape** (*2-tuple* *of* [*int*](https://docs.python.org/3/library/functions.html#int)) – The shape of the image `(height, width)`. If *image* is an array, the *shape* is used to confirm it has the correct shape. If *image* is a constant, *shape* is used to generate the virtual constant image. * **num_channels** ([*int*](https://docs.python.org/3/library/functions.html#int)) – The number of color channels in the image. If *image* is an array, the *num_channels* is used to confirm it has the correct number of channels. If *image* is a constant, *num_channels* is used to generate the virtual constant image. * **depth** ([*enums.ColorDepth*](pytoshop.enums.md#pytoshop.enums.ColorDepth)) – The bit depth of the image. See [`enums.ColorDepth`](pytoshop.enums.md#pytoshop.enums.ColorDepth). If *image* is an array, the *depth* is used to confirm it has the correct number of channels. If *image* is a constant, *depth* is used to generate the virtual constant image. * **version** ([*enums.Version*](pytoshop.enums.md#pytoshop.enums.Version)) – The version of the PSD file. See [`enums.Version`](pytoshop.enums.md#pytoshop.enums.Version). """ pass ``` -------------------------------- ### Objects API Source: https://github.com/mdboom/pytoshop/blob/master/docs/api.md Defines the core objects and data structures used within PSD files. ```APIDOC ## Objects API - Blending Range ### Manage Blending Ranges Provides functionality to manage blending ranges within PSD files. ### Method N/A (Module/Class) ### Endpoint N/A ### Parameters None ### Request Example ```python import pytoshop.blending_range # Example usage (specific methods depend on the module's implementation) # blending_info = pytoshop.blending_range.BlendingRangeInfo() ``` ### Response N/A ``` ```APIDOC ## Objects API - Core ### Core Objects (PsdFile, Header) Contains the fundamental objects for representing a PSD file, including the main `PsdFile` object and its `Header`. ### Method N/A (Module/Class) ### Endpoint N/A ### Parameters None ### Request Example ```python import pytoshop.core # Accessing the PsdFile class # psd_file = pytoshop.core.PsdFile() # Accessing the Header class # header = pytoshop.core.Header() ``` ### Response N/A ``` ```APIDOC ## Objects API - Color Mode Data ### Color Mode Data Section Represents the `ColorModeData` section of a PSD file. ### Method N/A (Module/Class) ### Endpoint N/A ### Parameters None ### Request Example ```python import pytoshop.color_mode # Example usage # color_mode_data = pytoshop.color_mode.ColorModeData() ``` ### Response N/A ``` ```APIDOC ## Objects API - Image Data ### Image Data Section Represents the `ImageData` section of a PSD file. ### Method N/A (Module/Class) ### Endpoint N/A ### Parameters None ### Request Example ```python import pytoshop.image_data # Example usage # image_data = pytoshop.image_data.ImageData() ``` ### Response N/A ``` ```APIDOC ## Objects API - Image Resources ### Image Resources Section Represents the `ImageResources` section of a PSD file. ### Method N/A (Module/Class) ### Endpoint N/A ### Parameters None ### Request Example ```python import pytoshop.image_resources # Example usage # image_resources = pytoshop.image_resources.ImageResources() ``` ### Response N/A ``` ```APIDOC ## Objects API - Layers ### Layers Sections Contains sections related to image layers within a PSD file. ### Method N/A (Module/Class) ### Endpoint N/A ### Parameters None ### Request Example ```python import pytoshop.layers # Example usage # layer_section = pytoshop.layers.Layers() ``` ### Response N/A ``` ```APIDOC ## Objects API - Path ### Path Handling Provides functionality to handle Bézier paths within PSD files. ### Method N/A (Module/Class) ### Endpoint N/A ### Parameters None ### Request Example ```python import pytoshop.path # Example usage # bezier_path = pytoshop.path.Path() ``` ### Response N/A ``` ```APIDOC ## Objects API - Tagged Block ### Tagged Block Objects Represents `TaggedBlock` objects used in PSD files. ### Method N/A (Module/Class) ### Endpoint N/A ### Parameters None ### Request Example ```python import pytoshop.tagged_block # Example usage # tagged_block = pytoshop.tagged_block.TaggedBlock() ``` ### Response N/A ``` -------------------------------- ### Unicode String Handling Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.util.md Utilities for encoding and decoding Unicode strings according to Photoshop's format. ```APIDOC ## pytoshop.util.decode_unicode_string ### Description Decode Photoshop’s definition of a Unicode String. ### Method N/A (Function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) - **unicode_string** (unicode) - The decoded Unicode string. #### Response Example N/A ``` ```APIDOC ## pytoshop.util.encode_unicode_string ### Description Encode Photoshop’s definition of a Unicode String. ### Method N/A (Function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) - **bytes** (bytes) - The encoded byte string. #### Response Example N/A ``` -------------------------------- ### Logging and Debugging Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.util.md Utilities for logging messages and debugging read/write operations. ```APIDOC ## pytoshop.util.log ### Description Print a logging message if debugging is turned on. ### Method N/A (Function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` ```APIDOC ## pytoshop.util.trace_read ### Description Prints debugging information from a read or write method. For internal use only. ### Method N/A (Decorator) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` ```APIDOC ## pytoshop.util.trace_write ### Description Prints debugging information from a read or write method. For internal use only. ### Method N/A (Decorator) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Write Constant Image Data (ZIP Compression) Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.codecs.md Writes a virtual image containing a constant value to a ZIP compressed stream. It requires a file-like object, the constant value, image dimensions (width, rows), color depth, and PSD version. ```python def compress_constant_zip(fd: BinaryIO, value: int, width: int, rows: int, depth: enums.ColorDepth, version: enums.Version) -> None: """Write a virtual image containing a constant to a zip compressed stream.""" pass ``` -------------------------------- ### Accessing Image Resources Source: https://context7.com/mdboom/pytoshop/llms.txt Read metadata and image resources from PSD files, such as resolution info, EXIF data, and ICC profiles. ```APIDOC ## Accessing Image Resources Read metadata and image resources from PSD files. ### Description This section demonstrates how to access various image resource blocks embedded within a PSD file. These resources contain metadata like resolution information, EXIF data, XMP metadata, ICC profiles, and thumbnail data. ### Usage **Reading a PSD file and accessing resources:** ```python import pytoshop from pytoshop import enums with open('image.psd', 'rb') as fd: psd = pytoshop.read(fd) resources = psd.image_resources ``` **Getting a specific resource block by ID:** ```python # Example: Get layer group information layers_group_info = resources.get_block(enums.ImageResourceID.layers_group_info) if layers_group_info: print(f"Layer group IDs: {layers_group_info.group_ids}") ``` ### Common Resource IDs - **Resolution Info**: `enums.ImageResourceID.resolution_info` - **Alpha Channel Names**: `enums.ImageResourceID.alpha_channel_names` - **Copyright Flag**: `enums.ImageResourceID.copyright_flag` - **EXIF Data**: `enums.ImageResourceID.exif_data_1` - **XMP Metadata**: `enums.ImageResourceID.xmp_metadata` - **ICC Profile**: `enums.ImageResourceID.icc_profile` - **Thumbnail**: `enums.ImageResourceID.thumbnail_resource` - **Slices**: `enums.ImageResourceID.slices` ``` -------------------------------- ### Bit Packing Source: https://github.com/mdboom/pytoshop/blob/master/docs/_generated/pytoshop.util.md Utility for packing boolean flags into an integer bit field. ```APIDOC ## pytoshop.util.pack_bitflags ### Description Pack separate booleans back into a bit field. ### Method N/A (Function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) - **int** (int) - The packed integer representing the bit flags. #### Response Example N/A ```