from aspose.psd import Image
from aspose.psd.fileformats.psd import PsdImage
from aspose.psd.fileformats.psd.layers.smartobjects import SmartObjectLayer
from aspose.psd.imageloadoptions import PsdLoadOptions
from aspose.psd.imageoptions import PngOptions
def find_smart_layers(psd_file_path):
with Image.load(psd_file_path) as image:
if isinstance(image, PsdImage):
psd_image = image
print(f"Searching for smart layers in: {psd_file_path}“)
found_smart_layers = False
for i, layer in enumerate(psd_image.layers):
if isinstance(layer, SmartObjectLayer):
print(f” Found Smart Object Layer: ‘{layer.name}’ at index {i}“)
found_smart_layers = True
if not found_smart_layers:
print(” No Smart Object Layers found in this PSD file.“)
else:
print(f"The file ‘{psd_file_path}’ is not a valid PSD image.”)
It seems you’re encountering an issue where Aspose.PSD is unable to recognize your PSD files as valid. Here are a few steps you can take to troubleshoot this problem:
File Integrity: Ensure that the PSD files are not corrupted. You can try opening them in Adobe Photoshop or another image editing software to verify their integrity.
File Format: Make sure that the files are indeed in the PSD format. Sometimes, files may have a .psd extension but are not actually in the correct format.
Library Version: Check if you are using the latest version of the Aspose.PSD library. If not, consider updating to the latest version as it may contain bug fixes and improvements.
Sample Code: Here’s a simple example of how to load a PSD file using Aspose.PSD in Python:
import aspose.psd as psd
# Load the PSD file
try:
image = psd.Image.load("D:\\Healthy\\Design\\Mockups\\LandscapeMockups\\Automation\\001.psd")
print("PSD file loaded successfully.")
except Exception as e:
print(f"Error: {e}")
Testing with Different Files: If possible, test with different PSD files to see if the issue persists across all files or is specific to certain ones.
If you continue to face issues, consider reaching out to Aspose support or checking their forums for similar problems and solutions.
import aspose.psd as psd
from aspose.psd import Image
from aspose.psd.fileformats.psd import PsdImage
from aspose.psd.fileformats.psd.layers.smartobjects import SmartObjectLayer
from aspose.psd.imageloadoptions import PsdLoadOptions
from aspose.psd.imageoptions import PngOptions
def find_smart_layers(psd_file_path):
try:
image = psd.Image.load(psd_file_path)
except Exception as e:
print(f"Error: {e}")
if isinstance(image, PsdImage):
psd_image = image
print(f"Searching for smart layers in: {psd_file_path}")
found_smart_layers = False
for i, layer in enumerate(psd_image.layers):
if isinstance(layer, SmartObjectLayer):
print(f" Found Smart Object Layer: '{layer.name}' at index {i}")
found_smart_layers = True
if not found_smart_layers:
print(" No Smart Object Layers found in this PSD file.")
else:
print(f"The file '{psd_file_path}' is not a valid PSD image.")
else:
print("not image instance")
Usually, using Aspose.PSD for Python via .NET the following cast is needed:
# Open File using Aspose.PSD for Python
with Image.load(sourceFile, loadOptions) as image:
# Types of Aspose.PSD can be casted
psdImage = cast(PsdImage, image)
# To check the class of objeectg please try this:
layer = psdImage.layers[3]
if is_assignable(layer, SmartObjectLayer):
print(f" Found Smart Object Layer: '{layer.name}' at index 3")
If the issue still presented after this code, please provide information about you environment, version of Aspose.PSD and please provide the input file to help us to reproduce the issue.
@blstraub please check that the Aspose.PSD for Python is installed in the same Python environment in which you try to run examples. Run in the your Python IDE the following consoole command:
Dmitriy: I, too, suspected that the Python installation on this test machine was part of the problem.
I deleted Python and all of its resources and then reinstalled Python, VS Code and AsposePSD.
The errors that I were getting before continue. This simple program:
from typing import cast from aspose.psd import Image from aspose.psd.imageloadoptions import PsdLoadOptions from aspose.psd.fileformats.psd import PsdImage
with Image.load(file_path, load_options) as image: psdImage = cast(PsdImage, image)
for layer in psdImage.layers: print(f"Layer Name: {layer.name}")
image.dispose()
returns the following error message:
Traceback (most recent call last):
** File “d:\Healthy\Python\layer test 2.py”, line 17, in **
** for layer in psdImage.layers:**
** ^^^^^^^^^^^^^^^** AttributeError: ‘aspose.psd.Image’ object has no attribute ‘layers’
There are three underlying problems: the imports for aspose.psd, aspose.psd.imageloadoptions and aspose.psd.fileformats.psd could not be resolved by Pylance.
Suggestions?
And the obvious question: Does AsposePDS actually work with Python in a Windows machine?