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

Skip to content

When replacing a PNG image inside a PSD file, the replaced PNG ends up with a background color and the colors are chaotic. #477

@liuwuqingnian

Description

@liuwuqingnian

`

try:
    psd = PSDImage.open(psd_path)
    
    logo_layer = psd.find("logo")
    if logo_layer:
        
        try:
            psd.remove(logo_layer)
            
            pil_im = Image.open(replaceimg_path)
            pil_im = pil_im.convert("RGBA")

            top = getattr(logo_layer, 'top', 0)
            left = getattr(logo_layer, 'left', 0)

            new_layer = PixelLayer.frompil(
                pil_im,
                psd_file=psd,
                layer_name="logo_new",
                top=top,
                left=left
            )
            parent = logo_layer.parent
            if parent is not None:
                parent.append(new_layer)
                new_layer.composite(force=True)
            else:
                print("❌")
            psd.save(new_psd_path)
            print(f"💾 : {new_psd_path}")
        except Exception as e:
            print(f"❌ : {e}")
    else:
        print("❌ ")
        
except Exception as e:
    print(f"❌ : {e}")
    import traceback
    traceback.print_exc()

Image

How can I make the transparent parts of the replaced PNG image display correctly?

python verision: 3.11.11
psd_tools version: 1.10.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions