Skip to content

Commit 0b26e26

Browse files
committed
docs(examples): add new examples
1 parent 3cbdc29 commit 0b26e26

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

examples/export_layers_as_png.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def main():
2222
with Session(psd_file, action="open") as ps:
2323
doc = ps.active_document
2424
options = ps.PNGSaveOptions()
25+
options.compression = 1
2526
layers = doc.artLayers
2627
for layer in layers:
2728
hide_all_layers(layers)
@@ -31,7 +32,7 @@ def main():
3132
if not os.path.exists(layer_path):
3233
os.makedirs(layer_path)
3334
image_path = os.path.join(layer_path, f"{layer.name}.png")
34-
doc.saveAs(image_path, options, True)
35+
doc.saveAs(image_path, True, options)
3536
ps.alert("Task done!")
3637
ps.echo(doc.activeLayer)
3738

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Export every layer as a .png file use `ExportOptionsSaveForWeb`."""
2+
# Import built-in modules
3+
import os
4+
5+
# Import third-party modules
6+
import examples._psd_files as psd # Import from examples.
7+
8+
# Import local modules
9+
from photoshop import Session
10+
11+
PSD_FILE = psd.get_psd_files()
12+
13+
14+
def hide_all_layers(layers):
15+
for layer in layers:
16+
layer.visible = False
17+
18+
19+
def main():
20+
psd_file = PSD_FILE["export_layers_as_png.psd"]
21+
with Session(psd_file, action="open") as ps:
22+
doc = ps.active_document
23+
options = ps.ExportOptionsSaveForWeb()
24+
layers = doc.artLayers
25+
for layer in layers:
26+
hide_all_layers(layers)
27+
layer.visible = True
28+
layer_path = os.path.join(doc.path, layer.name)
29+
print(layer_path)
30+
if not os.path.exists(layer_path):
31+
os.makedirs(layer_path)
32+
image_path = os.path.join(layer_path, f"{layer.name}.png")
33+
doc.exportDocument(image_path, exportAs=ps.ExportType.SaveForWeb, options=options)
34+
ps.alert("Task done!")
35+
ps.echo(doc.activeLayer)
36+
37+
38+
if __name__ == "__main__":
39+
main()

0 commit comments

Comments
 (0)