psd-tools / psd-tools

Python package for reading Adobe Photoshop PSD files
https://psd-tools.readthedocs.io/
MIT License
1.13k stars 189 forks source link

Can't save embedded psd file, or replace the image. #352

Open CptJackieSparrow opened 1 year ago

CptJackieSparrow commented 1 year ago

Describe the bug So i'm opening a Master.psd file, includes a smart layer called "Layer 1" Where layer 1 has a rectangle. This is a mockup script, or trying to be. I'm locating the smartobject without problem with:

smart_object_layer = None
    for layer in psd:
        print("Layer: {}, Kind: {}".format(layer.name, layer.kind))
        if layer.kind == 'group':
            for sub_layer in layer.layers:
                print("    Sub-layer: {}, Kind: {}".format(sub_layer.name, sub_layer.kind))
                if sub_layer.kind == 'smartobject':
                    smart_object_layer = sub_layer
                    break
            if smart_object_layer:
                break
        elif layer.kind == 'smartobject':
            smart_object_layer = layer
            break
    if smart_object_layer is None:
        print("No smart object layer found")
    else:
        print("Found smart object layer: {}".format(smart_object_layer.name))

And it's working great. But when i try to replace the contents of the smart layer (when i try to import an png image) with this:

embedded_psd = smart_object_layer.smart_object
        with BytesIO(embedded_psd.data) as f:
            embedded_psd = psd_tools.PSDImage.open(f)

        # Replace the contents of the smart object layer with the contents of output.png
        embedded_psd[0].compose(img)
        embedded_psd.save(smart_object_layer.name)

I can't seem to import the image Plus i can't save the embedded psd (which is "Layer 1.psd").

when i:

output_folder = os.path.join(processed_folder, f"Mockups_{image[:5]}")
        os.makedirs(output_folder, exist_ok=True)
        output_file = os.path.join(output_folder, f"{image[:5]}_{mockup_file[:-4]}.png")
        psd.composite().save(output_file)

And even when i try to change the compose to composite, nothing changes. Am I doing something wrong or this is a limitation?

Environment

ranareehanaslam commented 1 year ago

let me know if there is any fix

grandepuffoa commented 2 months ago

did you find any solution?

CptJackieSparrow commented 2 months ago

Nope, unfortunately not. I think the python is not the way to go when it comes to psd document editing. Though I don't really understand javascript.