psd-tools
psd-tools copied to clipboard
ValueError when composite() a layer with colors: Greyscale and CMYK
Describe the bug I see a ValueError when compositing vector layers with Greyscale foreground / fill and CMYK background / outline colors.
Please extract the attached minimal.zip
containing test.psd
, and use the script below to reproduce.
I speculate psd-tools is having difficulty with vector rectangle because it has CMYK outline and Grayscale inner fill. The stacktrace complains about dimensionality, probably RGB vs RGBA, when converting.
Changing the colors to HSL, and re-exporting, fixes the problem.
- NOTE: An earlier version speculated layers with vector art were the problem. However, I discovered layers with HSL color palette were compatible with psd-tools.
- NOTE: An earlier version of this bug speculated that an empty Group layer was the issue. However, that layer is simply the first visible layer containing vector art.
- NOTE: Visible PixelLayers do not appear to have issues.
To Reproduce
- Extract either of the attached archives -
minimal.zip
is faster. Findtest.psd
inside. - Execute the following script
- See exception
from psd_tools import PSDImage
psd = PSDImage.open('test.psd')
psd.composite().save('test.png')
for layer in psd:
print(layer)
if layer.name != '':
layer_image = layer.composite()
layer_image.save('%s.png' % layer.name)
See error trace ending in:
...
File "/Users/nick/proj/qun/psd-in-md/venv/lib/python3.11/site-packages/psd_tools/composite/__init__.py", line 261, in _apply_source
color_t = (shape - alpha) * alpha_b * color_b + alpha * (
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: operands could not be broadcast together with shapes (240,186,3) (240,186,4)
Expected behavior No error.
File and screenshots
- PSD with 1 layer demonstrating the issue - note the color details of the outline and the fill
OLD files
- PSD and explanatory README.md with full stack-trace
- Problematic layer is "BG", I think
-
Environment
- Python [3.11]
- Version [v1.9.31]
- macOS 14.2.1
- See README in ZIP for more versions.
Additional context The PSD was generated originally in Affinity Designer 2.4.1.
Thank you for maintaining such a useful package. Reading PSD files in Python gives me a lot of power.
I plan to use this to help me maintain a piece of user documentation for a music synthesizer, (https://github.com/raspy135/Qun-mk2/pull/27) generating PNG images from the PSD layers in different combinations, where each layer contains an illustration of just one of the ~20 physical controls on the synthesizer. I want to script the creation of custom images, and perhaps GIFs, illustrating the button sequences needed to execute commands into the synthesizer. Operating the synthesizer requires pressing multiple buttons all at once, in different combinations. So, me programmatically generating images that highlight the relevant buttons for a given feature should help make it easier to write the document document for users to better understand what is involved for them to activate the different features, beyond just reading the text in the manual.