-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.gd
223 lines (180 loc) · 6.54 KB
/
Main.gd
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
extends Node
#How this program works:
#1. Place images you wish to edit in import folder.
#2. Create a procedure file that edits all pictures.
#2.1. Allow usage of asset files (watermarks, effects) to be used, stored in assets folder.
#3. Run program directly, or through the command line if more options are desired.
#3.1 If program is run directly, procedure file named "default.gdpie" will be used
#4. processed images will be output with the same name, in an export folder
onready var exe_path : String = OS.get_executable_path().get_base_dir()
onready var import_path : String = exe_path + "/import/"
onready var export_path : String = exe_path + "/export/"
onready var procedure_path : String = exe_path + "/procedures/"
onready var asset_path : String = exe_path + "/assets/"
var assets : Dictionary = {}
func _ready() -> void:
var args : Array = OS.get_cmdline_args()
main(args)
#close program
get_tree().quit()
func main(args : Array) -> void:
#allow folder paths to be overriden
#check that all folders are present, if not, make new folder
var dir := Directory.new()
var import_path_supplied : bool = args.has("-i")
if import_path_supplied:
var path_index : int = args.find("-i") + 1
if args.size() >= path_index:
import_path = args[path_index]
var import_folder_exists : bool = dir.dir_exists(import_path)
if !import_folder_exists:
var err : int = dir.make_dir(import_path)
if err != OK:
push_error("Error %s when making import folder." % err)
var export_path_supplied : bool = args.has("-e")
if export_path_supplied:
var path_index : int = args.find("-e") + 1
if args.size() >= path_index:
export_path = args[path_index]
var export_folder_exists : bool = dir.dir_exists(export_path)
if !export_folder_exists:
var err : int = dir.make_dir(export_path)
if err != OK:
push_error("Error %s when making export folder." % err)
var procedure_path_supplied : bool = args.has("-p")
var procedure_folder_exists : bool = dir.dir_exists(procedure_path)
if !procedure_folder_exists:
var err : int = dir.make_dir(procedure_path)
if err != OK:
push_error("Error %s when making procedures folder" % err)
var asset_path_supplied : bool = args.has("-a")
if export_path_supplied:
var path_index : int = args.find("-a") + 1
if args.size() >= path_index:
export_path = args[path_index]
if !export_folder_exists:
var err : int = dir.make_dir(export_path)
if err != OK:
push_error("Error %s when making export folder." % err)
var asset_folder_exists : bool = dir.dir_exists(asset_path)
if !asset_folder_exists:
var err : int = dir.make_dir(asset_path)
if err != OK:
push_error("Error %s when making assets folder" % err)
#load procedure
var contents : String = ""
if procedure_path_supplied:
var path_index : int = args.find("-p") + 1
if args.size() >= path_index:
contents = load_procedure(args[path_index])
#no procedure specified, use default.procedure
else:
#check that default procedure exists
var file := File.new()
if !file.file_exists(procedure_path + "default.gdpie"):
create_default_procedure()
contents = load_procedure(procedure_path + "default.gdpie")
var procedure_steps : Array = []
#format procedure
var lines : PoolStringArray = contents.split("\n", false)
#load commands into array
for line in lines:
var comment : Array = line.split("#", false, 1)
procedure_steps.append(comment[0].split(" ", false))
#load assets
var assets := get_contents(asset_path)
load_assets(assets)
#load images (one at a time)
var images := get_contents(import_path)
for path in images:
#get name of file without any overlapping filetypes
var base_path : String = path.left(path.find("."))
var image : Image = Image.new()
image.load(import_path + path)
#process image through procedure steps
for step in procedure_steps:
image = interpreter(image, step, export_path + base_path)
#returns list of contents of a folder
func get_contents(path : String) -> PoolStringArray:
var list := PoolStringArray()
var dir := Directory.new()
var err : int = dir.open(path)
if err != OK:
assert(false, "Error %s occurred when trying to access %s" % [err, path])
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if !dir.current_is_dir():
list.append(file_name)
file_name = dir.get_next()
return list
func load_procedure(path : String) -> String:
var file := File.new()
#file not found
if !file.file_exists(path):
assert(false, "File not found at %s" % path)
var err : int = file.open(path, File.READ)
if err != OK:
assert(false, "Error %s occurred when trying to access %s" % [err, path])
var content := file.get_as_text()
file.close()
return content
#change this so that the procedure is parsed, all necessary assets are found, and load them selectively
func load_assets(files : PoolStringArray) -> int:
for file in files:
assets[file] = load(asset_path + file)
return 0
func interpreter(image : Image, args : Array, path : String) -> Image:
match args[0]:
# "blend_image":
# image = blend_image(image, args)
"crop":
if args.size() < 5:
incorrect_number_args_error(args)
var size := image.get_size()
#crop right and bottom
image.crop(size.x - int(args[3]), size.y - int(args[4]))
#flip
image.flip_x()
image.flip_y()
#crop left and top
image.crop(size.x - int(args[1]) - int(args[3]), size.y - int(args[2]) - int(args[4]))
#return to original orientation
image.flip_x()
image.flip_y()
"expand_x2_hq2x":
image.expand_x2_hq2x()
"flip_x":
image.flip_x()
"flip_y":
image.flip_y()
"resize":
if args.size() < 4:
incorrect_number_args_error(args)
image.resize(int(args[1]), int(args[2]), int(args[3]))
"resize_to_po2":
if args.size() < 3:
incorrect_number_args_error(args)
image.resize_to_po2(bool(args[1]), int(args[2]))
"save_exr":
if args.size() < 2:
incorrect_number_args_error(args)
image.save_exr(path, bool(args[1]))
"save_png":
image.save_png(path)
"save_png_as_gif":
image.save_png(path + ".gif")
"shrink_x2":
image.shrink_x2()
var cmd:
push_error("Unsupported command %s" % cmd)
return image
func blend_image(image : Image, args : Array) -> Image:
return image
func incorrect_number_args_error(args : Array) -> void:
push_error("Incorrect number of arguments at %s" % args)
func create_default_procedure() -> void:
var file = File.new()
file.open(procedure_path + "default.gdpie", File.WRITE)
file.store_line("crop 10 10 10 10 #crops 10 pixels from the left, top, right, and bottom respectively")
file.store_line("save_png #saves image")