-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpsd2img.py
68 lines (44 loc) · 1.76 KB
/
psd2img.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import argparse
import os
from psd_tools import PSDImage
def pngCreator(files, img_extension):
for file in files:
# Iterate through the files
if os.path.isfile(f'{os.getcwd()}\{file}'):
# Get the base filename
filename_full = os.path.basename(file)
# Get the filename and extension
filename, extension = os.path.splitext(filename_full)
# Check the extension of file is psd
if extension == '.psd':
# Open the file
psd = PSDImage.open(file)
# Save the file according to extension provided
psd.composite().save(f'{filename}.{img_extension}')
print(f'File created: {filename}.{img_extension}')
else:
print(f'Extension NOT Valid for file: {file}')
def main():
parser = argparse.ArgumentParser(description='Convert PSD to JPEG, PNG')
# Argument is to accept a file(s)
parser.add_argument('-f', dest='file', nargs='+', type=str, help='Submit a PSD file(s)', required=True)
# Argument is to convert psd file to jpeg
parser.add_argument('-j', dest='jpeg', action='store_true', help='Convert PSD to JPEG')
# Argument is to convert psd file to png
parser.add_argument('-p', dest='png', action='store_true', help='Convert PSD to PNG')
args = parser.parse_args()
if args.file:
# Accepts files
files = args.file
# JPEG parameter is selected
if args.jpeg:
pngCreator(files, 'jpeg')
# PNG parameter is selected
elif args.png:
pngCreator(files, 'png')
else:
print('See Help: psd2img.py -h')
else:
print('No Files Given')
if __name__ == '__main__':
main()