Thanks to visit codestin.com
Credit goes to github.com

Skip to content

How do I save PSD which converted from nested_layers WITHOUT any raster masks? #30

@ppooiiuuyh

Description

@ppooiiuuyh

Description

When saving a psd converted from nested layers created from scratch using a numpy image, unexpected raster masks was created with it. Is there any way to save a psd file without any raster masks?

What I Did

Initially, the raster mask value was set to 0, so the image was not visible.

image

I could solve this by changing the parameter value of the layer mask class below. But still I couldn't find a way to remove the raster masks.

# pytoshop -> layers -> LayerMask
class LayerMask(object):
    """
    Layer mask / adjustment layer data.
    """
    def __init__(self,
                 top=0,                                      # type: int
                 left=0,                                     # type: int
                 bottom=0,                                   # type: int
                 right=0,                                    # type: int
                 default_color=True,#<- False            # type: bool
                 position_relative_to_layer=False,           # type: bool
                 layer_mask_disabled=True, #<- False    # type: bool
                 invert_layer_mask_when_blending=False,      # type: bool

image

Here is my entire code,

import pytoshop.user.nested_layers
import pytoshop.enums
import os
import cv2

def save_decomposed_psd(dst_path, s=None,su=None):
    def gen_layer(img, name):
        if img.shape[-1] ==3 :
            img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)

        layer = pytoshop.user.nested_layers.Image(name=name, group_id=0, top=0, left=0, bottom=img.shape[0], right=img.shape[1], channels=None, metadata=None, layer_color=0, color_mode=3)
        ca = pytoshop.layers.ChannelImageData(image=img[...,-1])
        cr = pytoshop.layers.ChannelImageData(image=img[...,2])
        cg = pytoshop.layers.ChannelImageData(image=img[...,1])
        cb = pytoshop.layers.ChannelImageData(image=img[...,0])
        layer.channels = {-1:ca, 0:cr, 1:cg, 2:cb}
        return layer

    def gen_group(layers, name):
        group = pytoshop.user.nested_layers.Group(name=name, group_id=0, layers=layers)
        return group

    psd_nl = [gen_group([gen_layer(s,"s"), gen_layer(su,"su")], "gs")]

    psd = pytoshop.user.nested_layers.nested_layers_to_psd(psd_nl, color_mode=3)
    with open(dst_path, 'wb') as fd:
        psd.write(fd)


if __name__ == "__main__":
    root = "/root/results/psd_tools_practice"
    s = cv2.imread(os.path.join(root,"a.png"), cv2.IMREAD_UNCHANGED)
    su = cv2.imread(os.path.join(root,"b.png"), cv2.IMREAD_UNCHANGED)

    dst = os.path.join(root,"decoposed_psd")
    save_decomposed_psd(dst,s)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions