Skip to content

Instantly share code, notes, and snippets.

@arachonteur
Created October 22, 2022 01:10
Show Gist options
  • Save arachonteur/3741f762cb712ae840ca3b89aa3a4e82 to your computer and use it in GitHub Desktop.
Save arachonteur/3741f762cb712ae840ca3b89aa3a4e82 to your computer and use it in GitHub Desktop.
Python script for exporting layers from a PSD file
from psd_tools import PSDImage
import sys, os
def parse_layers(group, current_dir = ""):
for layer in group:
if layer.is_group():
target_dir = (current_dir + "\\" + layer.name).replace(chr(0), "")
if not os.path.isdir(target_dir):
os.mkdir(target_dir)
parse_layers(layer, current_dir + "/" + layer.name)
else:
if(layer.kind == "pixel"):
image = layer.composite()
target_file = "%s/%s.png" % (current_dir, "".join(e for e in layer.name if e.isalnum()))
image.save(target_file.replace(chr(0), ""))
def main() -> int:
target_file = "Test.psd"
if(len(sys.argv) >= 2):
target_file = sys.argv[1]
else:
print("Input PSD file: ")
target_file = input()
psd = PSDImage.open(target_file)
if(not psd):
print(u"Error: No shit given.")
quit(0)
else:
print("Opening PSD file... Please wait.")
target_dir = os.path.splitext(target_file)[0].replace(chr(0), "")
if not os.path.isdir(target_dir):
os.mkdir(target_dir)
parse_layers(psd, os.path.splitext(target_file)[0])
return 0
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment