|
1 | 1 | """Replace the image of the current active layer with a new image."""
|
2 | 2 |
|
| 3 | +# Import third-party modules |
| 4 | +import examples._psd_files as psd # Import from examples. |
3 | 5 |
|
4 | 6 | # Import local modules
|
5 | 7 | from photoshop import Session
|
6 | 8 |
|
7 | 9 |
|
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"] |
9 | 18 | replace_contents = ps.app.stringIDToTypeID("placedLayerReplaceContents")
|
10 | 19 | desc = ps.ActionDescriptor
|
11 | 20 | idnull = ps.app.charIDToTypeID("null")
|
12 |
| - desc.putPath(idnull, "your/image/path.jpg") |
| 21 | + desc.putPath(idnull, input_file) |
13 | 22 | 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