aspose-psd / Aspose.PSD-for-Python-Net

Aspose.PSD for Python examples, plugins and showcases
MIT License
4 stars 0 forks source link

layer_mask = LayerMaskData() TypeError: type has no available constructor #3

Open yaju1234 opened 1 month ago

yaju1234 commented 1 month ago

When creating the object layer_mask = LayerMaskData() it gives error TypeError: type has no available constructor. Please help me . I have a original image and mask image . I have added the original image in a layer and want to create a layer mask and to to add the mask image in that layer mask. But layer_mask = LayerMaskData() is not working. I am using ubuntu22.04 and python3.10

yaju1234 commented 1 month ago

Here is the full source code from io import BytesIO from aspose.psd.fileformats.psd import PsdImage from aspose.psd.fileformats.psd.layers import Layer, LayerMaskData

from PIL import Image

inputFile = "original.jpg" maskFile = "alpha.png" outputFile = "AddFileAsLayer.psd"

Read original image and mask image data

with open(inputFile, "rb") as file: original_image_data = file.read()

with open(maskFile, "rb") as file: mask_image_data = file.read()

Open images using PIL

img = Image.open(inputFile) mask = Image.open(maskFile).convert("L") img.putalpha(mask)

Convert img to bytes

imgByteArr = BytesIO() img.save(imgByteArr, format='PNG') imgByteArr = imgByteArr.getvalue()

Create the original layer

original_layer = Layer(BytesIO(original_image_data))

Create the transparent layer with the alpha channel

transparent_layer = Layer(BytesIO(imgByteArr))

Create the mask layer

mask_layer = Layer(BytesIO(mask_image_data)) mask_layer.is_visible = False

Create a layer mask for the original layer

layer_mask = LayerMaskData() layer_mask.image_data = mask_image_data original_layer.layer_mask_data = layer_mask

Create a new PSD image with the same dimensions as the original image

psd_image = PsdImage(original_layer.width, original_layer.height)

Add the layers to the PSD image

psd_image.layers = [original_layer, mask_layer, transparent_layer]

Save the PSD file

psd_image.save(outputFile) alpha original