Skip to content

Commit 2e18eef

Browse files
committed
chore(examples): improve example for replace image
1 parent 6452f6b commit 2e18eef

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

examples/files/green_130x260.png

2.13 KB
Loading

examples/files/red_100x200.png

1.96 KB
Loading

examples/files/replace_images.psd

430 KB
Binary file not shown.

examples/replace_images.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
"""Replace the image of the current active layer with a new image."""
22

3+
# Import third-party modules
4+
import examples._psd_files as psd # Import from examples.
35

46
# Import local modules
57
from photoshop import Session
68

79

8-
with Session() as ps:
10+
PSD_FILE = psd.get_psd_files()
11+
12+
13+
with Session(PSD_FILE["replace_images.psd"], action="open") as ps:
14+
active_layer = ps.active_document.activeLayer
15+
bounds = active_layer.bounds
16+
print(f"current layer {active_layer.name}: {bounds}")
17+
input_file = PSD_FILE["red_100x200.png"]
918
replace_contents = ps.app.stringIDToTypeID("placedLayerReplaceContents")
1019
desc = ps.ActionDescriptor
1120
idnull = ps.app.charIDToTypeID("null")
12-
desc.putPath(idnull, "your/image/path.jpg")
21+
desc.putPath(idnull, input_file)
1322
ps.app.executeAction(replace_contents, desc)
23+
24+
# replaced image.
25+
active_layer = ps.active_document.activeLayer
26+
current_bounds = active_layer.bounds
27+
width = bounds[2] - bounds[0]
28+
height = bounds[3] - bounds[1]
29+
30+
current_width = current_bounds[2] - current_bounds[0]
31+
current_height = current_bounds[3] - current_bounds[1]
32+
new_size = width / current_width * 100
33+
active_layer.resize(new_size, new_size, ps.AnchorPosition.MiddleCenter)
34+
print(f"current layer {active_layer.name}: {current_bounds}")

0 commit comments

Comments
 (0)