-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframemerger.py
executable file
·772 lines (629 loc) · 32.3 KB
/
framemerger.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
#!/usr/bin/python
#coding=utf8
##----------------------------------------------------------------------------##
## █ █ ##
## ████████ ##
## ██ ██ ##
## ███ █ █ ███ framemerger.py ##
## █ █ █ █ FrameMerger ##
## ████████████ ##
## █ █ Copyright (c) 2015, 2016 ##
## █ █ █ █ AmazingCow - www.AmazingCow.com ##
## █ █ █ █ ##
## █ █ N2OMatt - [email protected] ##
## ████████████ www.amazingcow.com/n2omatt ##
## ##
## This software is licensed as GPLv3 ##
## CHECK THE COPYING FILE TO MORE DETAILS ##
## ##
## Permission is granted to anyone to use this software for any purpose, ##
## including commercial applications, and to alter it and redistribute it ##
## freely, subject to the following restrictions: ##
## ##
## 0. You **CANNOT** change the type of the license. ##
## 1. The origin of this software must not be misrepresented; ##
## you must not claim that you wrote the original software. ##
## 2. If you use this software in a product, an acknowledgment in the ##
## product IS HIGHLY APPRECIATED, both in source and binary forms. ##
## (See opensource.AmazingCow.com/acknowledgment.html for details). ##
## If you will not acknowledge, just send us a email. We'll be ##
## *VERY* happy to see our work being used by other people. :) ##
## The email is: [email protected] ##
## 3. Altered source versions must be plainly marked as such, ##
## and must not be misrepresented as being the original software. ##
## 4. This notice may not be removed or altered from any source ##
## distribution. ##
## 5. Most important, you must have fun. ;) ##
## ##
## Visit opensource.amazingcow.com for more open-source projects. ##
## ##
## Enjoy :) ##
##----------------------------------------------------------------------------##
## Imports ##
import os;
import os.path;
import sys;
import getopt;
import pdb;
################################################################################
## Don't let the standard import error to users - Instead show a ##
## 'nice' error screen describing the error and how to fix it. ##
################################################################################
def __import_error_message_print(pkg_name, pkg_url):
print "Sorry, "
print "frame-merger depends on {} package.".format(pkg_name);
print "Visit {} to get it.".format(pkg_url);
print "Or checkout the README.md to learn other ways to install {}.".format(pkg_name);
exit(1);
## cowtermcolor ##
try:
from cowtermcolor import *;
except ImportError, e:
__import_error_message_print(
"cowtermcolor",
"http//opensource.amazingcow.com/cowtermcolor.html");
## pygame ##
#COWNOTE: Today we're using pygame to merge the photos. \
# But the desired is to use ImageMagick...
try:
import pygame;
except ImportError, e:
__import_error_message_print(
"pygame",
"http//www.pygame.org");
#For GUI :)
try:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
except ImportError, e:
__import_error_message_print(
"PyQt4",
"http://www.riverbankcomputing.com/software/pyqt/download");
################################################################################
## Constants ##
################################################################################
class Constants:
#Flags.
#Exclusive
FLAG_HELP = "h", "help";
FLAG_VERSION = "v", "version";
FLAG_GUI = "" , "gui";
#Mandatory
FLAG_FRAME_IMAGE_PATH = "f", "frame";
FLAG_IMAGES_DIR_PATH = "i", "images-dir";
FLAG_OUTPUT_DIR_PATH = "o", "output-dir";
#Optional
FLAG_FORCE_JPG = "", "jpg";
FLAG_NO_COLORS = "", "no-colors"
ALL_FLAGS_SHORT = "".join([
#Exclusive
FLAG_HELP [0],
FLAG_VERSION [0],
FLAG_GUI [0],
#Mandatory
FLAG_FRAME_IMAGE_PATH [0] + ":",
FLAG_IMAGES_DIR_PATH [0] + ":",
FLAG_OUTPUT_DIR_PATH [0] + ":",
#Optional
FLAG_FORCE_JPG[0],
FLAG_NO_COLORS[0],
]);
ALL_FLAGS_LONG = [
#Exclusive
FLAG_HELP [1],
FLAG_VERSION [1],
FLAG_GUI [1],
#Mandatory
FLAG_FRAME_IMAGE_PATH [1] + "=",
FLAG_IMAGES_DIR_PATH [1] + "=",
FLAG_OUTPUT_DIR_PATH [1] + "=",
#Optional
FLAG_FORCE_JPG[1],
FLAG_NO_COLORS[1],
];
#Image formats.
IMAGE_FORMATS = [".png", ".jpg", ".jpeg"];
#App.
APP_NAME = "frame-merger";
APP_VERSION = "0.2.2";
APP_AUTHOR = "N2OMatt <[email protected]>"
APP_COPYRIGHT = "\n".join(("Copyright (c) 2015, 2016 - Amazing Cow",
"This is a free software (GPLv3) - Share/Hack it",
"Check opensource.amazingcow.com for more :)"));
################################################################################
## GUI ##
################################################################################
class GUI(QWidget):
############################################################################
## Static Run Method ##
############################################################################
@staticmethod
def run(frame_path = "",
images_path = "",
output_path = "",
force_jpg = False):
#Never put term colors on gui.
ColorMode.mode = ColorMode.NEVER;
app = QApplication(sys.argv);
gui = GUI(frame_path, images_path, output_path, force_jpg);
gui.show();
sys.exit(app.exec_())
############################################################################
## CTOR ##
############################################################################
def __init__(self, frame_path = "",
images_path = "",
output_path = "",
force_jpg = False):
QWidget.__init__(self);
#UI Elements.
#Frame.
self.__frame_image_label = None;
self.__frame_image_text = None;
self.__frame_image_button = None;
#Images Dir.
self.__images_dir_label = None;
self.__images_dir_text = None;
self.__images_dir_button = None;
#Output Dir.
self.__output_dir_label = None;
self.__output_dir_text = None;
self.__output_dir_button = None;
#Merge Button.
self.__run_button = None;
#Layout.
self.__root_layout = None;
#Merge Progress Dialog.
self.__progress_dialog = None;
#The Merge Process object.
self.__merge_process = None;
#Initialize the UI.
self.init_ui();
#Set the Default values.
self.__frame_image_text.setText (frame_path );
self.__images_dir_text.setText (images_path);
self.__output_dir_text.setText (output_path);
self.__force_jpg_checkbox.setChecked(force_jpg );
############################################################################
## UI Initializer ##
############################################################################
def init_ui(self):
#Set the window properties.
self.setWindowTitle("{} - {} - {}".format(Constants.APP_NAME,
Constants.APP_VERSION,
"Amazing Cow"));
#Set the window properties.
self.setGeometry(0, 0, 480, 200);
#Create the widgets.
#Frame Image.
self.__frame_image_label = QLabel("Frame Image Path:");
self.__frame_image_text = QLineEdit();
self.__frame_image_text.textChanged.connect(self.__on_text_changed);
self.__frame_image_button = QPushButton("...");
self.__frame_image_button.clicked.connect(self.__on_frame_image_button_pressed);
#Images Dir.
self.__images_dir_label = QLabel("Images Directory Path:");
self.__images_dir_text = QLineEdit();
self.__images_dir_text.textChanged.connect(self.__on_text_changed);
self.__images_dir_button = QPushButton("...");
self.__images_dir_button.clicked.connect(self.__on_images_dir_button_pressed);
#Output Dir.
self.__output_dir_label = QLabel("Output Directory Path:");
self.__output_dir_text = QLineEdit();
self.__output_dir_text.textChanged.connect(self.__on_text_changed);
self.__output_dir_button = QPushButton("...");
self.__output_dir_button.clicked.connect(self.__on_output_dir_button_pressed);
#Run Button.
self.__run_button = QPushButton("Start merge!");
self.__run_button.setEnabled(False);
self.__run_button.clicked.connect(self.__on_run_button_pressed);
#Force JPG checkbox.
self.__force_jpg_checkbox = QCheckBox("Force save in JPG", self);
#Create the layout.
self.__root_layout = QGridLayout(self);
########################################################################
## GUI WILL LOOK LIKE THIS ##
## COL 1 COL2 ##
## +---------------------------+ ##
## ROW 1 | Frame Image Path | ##
## ROW 2 | [ ] ( ... ) | ##
## ROW 3 | Images Dir Path | ##
## ROW 4 | [ ] ( ... ) | ##
## ROW 5 | Output dir Path | ##
## ROW 6 | [ ] ( ... ) | ##
## ROW 7 | ( Start Merge ) [x] JPG | ##
## +---------------------------+ ##
########################################################################
#Add the widgets to layout.
#Frame Image.
self.__root_layout.addWidget(self.__frame_image_label, 1, 1);
self.__root_layout.addWidget(self.__frame_image_text, 2, 1);
self.__root_layout.addWidget(self.__frame_image_button, 2, 2);
#Images Dir.
self.__root_layout.addWidget(self.__images_dir_label, 3, 1);
self.__root_layout.addWidget(self.__images_dir_text, 4, 1);
self.__root_layout.addWidget(self.__images_dir_button, 4, 2);
#Output Dir.
self.__root_layout.addWidget(self.__output_dir_label, 5, 1);
self.__root_layout.addWidget(self.__output_dir_text, 6, 1);
self.__root_layout.addWidget(self.__output_dir_button, 6, 2);
#Merge Button.
self.__root_layout.addWidget(self.__run_button, 7, 1, 2, 1 );
#Force JPG checkbox.
self.__root_layout.addWidget(self.__force_jpg_checkbox, 7, 2, 2, 1 );
############################################################################
## Button Callbacks ##
############################################################################
def __on_frame_image_button_pressed(self):
#Create a string with the supported image formats to pass to QFileDialog.
#The string will have the format of Images Files (*.FMT1 *.FMT2 *.FMTN)
#This is need to ensure that user will selected a valid image format.
image_formats_str = "Image Files (*{})".format(" *".join(Constants.IMAGE_FORMATS));
file_selected = QFileDialog.getOpenFileName(self,
"Select Frame Image",
"",
image_formats_str);
self.__frame_image_text.setText(file_selected);
def __on_images_dir_button_pressed(self):
#The user press the ... button of the Images Dir.
#Set the Dialog to let user selected a directory that will
#be used to get the photos list.
dir_selected = QFileDialog.getExistingDirectory(self,
"Select Images Directory",
"",
QFileDialog.ShowDirsOnly);
self.__images_dir_text.setText(dir_selected);
def __on_output_dir_button_pressed(self):
#The user press the ... button of the Output Dir.
#Set the Dialog to let user selected a directory that will
#be used to place the merged photos.
dir_selected = QFileDialog.getExistingDirectory(self,
"Select Output Directory",
"",
QFileDialog.ShowDirsOnly);
self.__output_dir_text.setText(dir_selected);
def __on_run_button_pressed(self):
#Merge the photos and update the Progress Dialog if needed.
try:
self.__create_and_init_merge_process();
self.__create_and_init_process_dialog();
#Keep merging the photos and updating the progress interface
#until all photos are merged or user wants to quit.
while(self.__merge_process.has_image_to_merge()):
self.__merge_process.merge();
index = self.__merge_process.get_current_image_index()
self.__progress_dialog.setValue(index);
#User wants to quit.
if(self.__progress_dialog.wasCanceled()):
return;
#Set the all things are done.
images_count = self.__merge_process.get_images_count();
self.__progress_dialog.setValue(images_count);
#Show to user that processing was complete.
QMessageBox.information(self,
"Frame Merger",
"Processing complete...",
QMessageBox.Ok);
#This except block will catch all types of exceptions generated
#by the try block above and will show them in a "nice" Error Dialog
#to user.
except ValueError, e:
QMessageBox.critical(self,
"Frame Merger", #Just put the first part of,
e.args[0], #the error because the path
QMessageBox.Ok); #is obvious to user
except OSError, e:
msg = "{} - {}".format(e.strerror, e.filename);
QMessageBox.critical(self,
"Frame Merger",
msg,
QMessageBox.Ok);
############################################################################
## Text Box Callbacks ##
############################################################################
def __on_text_changed(self):
#We only enable the run button if user fills all the text boxes.
frame_len = len(self.__frame_image_text.text());
images_len = len(self.__images_dir_text.text());
output_len = len(self.__output_dir_text.text());
all_filled = all([frame_len, images_len, output_len]);
self.__run_button.setEnabled(all_filled);
############################################################################
## Helper Methods ##
############################################################################
def __create_and_init_merge_process(self):
self.__merge_process = MergeProcess();
self.__merge_process.set_frame_path (str(self.__frame_image_text.text()));
self.__merge_process.set_images_path(str(self.__images_dir_text.text ()));
self.__merge_process.set_output_path(str(self.__output_dir_text.text ()));
self.__merge_process.set_save_in_jpg(self.__force_jpg_checkbox.isChecked());
self.__merge_process.init();
def __create_and_init_process_dialog(self):
if(self.__progress_dialog is not None):
self.__progress_dialog.destroy();
self.__progress_dialog = None;
self.__progress_dialog = QProgressDialog("Merging Photos",
"Cancel",
0,
self.__merge_process.get_images_count(),
self);
self.__progress_dialog.setWindowModality(Qt.WindowModal);
self.__progress_dialog.forceShow();
################################################################################
## MergeProcess ##
################################################################################
class MergeProcess:
############################################################################
## CTOR ##
############################################################################
def __init__(self):
self.__frame_path = None; #Path of the Frame Image.
self.__images_path = None; #Path of dir that holds the images to be merged.
self.__output_path = None; #Path of dir that merge images will be placed.
self.__force_jpg = False;
self.__images_filenames = None; #Will hold only valid image filenames.
self.__current_photo_index = 0;
############################################################################
## Initializer Method ##
############################################################################
def init(self):
#We must assure that paths give are valid
#and that the list of image filenames contains
#only valid image filenames.
self.__check_paths();
self.__clean_images_filenames_list();
############################################################################
## Setters ##
############################################################################
def set_frame_path(self, path):
self.__frame_path = path;
def set_images_path(self, path):
self.__images_path = path;
def set_output_path(self, path):
self.__output_path = path;
def set_save_in_jpg(self, b):
self.__force_jpg = b;
############################################################################
## Merge Process State Getters ##
############################################################################
def get_images_count(self):
return len(self.__images_filenames);
def get_current_image_index(self):
return self.__current_photo_index;
def has_image_to_merge(self):
return self.__current_photo_index < self.get_images_count();
############################################################################
## Action Method ##
############################################################################
def merge(self):
#Grab the name of the current image.
#Canonize it's path and create the output path based in it's name.
#Next merge the photo and advance the image index.
image_filename = self.__images_filenames[self.__current_photo_index];
image_fullpath = os.path.join(self.__images_path, image_filename);
output_fullpath = os.path.join(self.__output_path, image_filename);
#If is to save in JPG, change the output filename to JPG.
if(self.__force_jpg):
output_fullpath = output_fullpath.replace(".png", ".jpg");
self.__merge_photo(image_fullpath, output_fullpath);
self.__current_photo_index += 1;
############################################################################
## Helper Methods ##
############################################################################
def __check_paths(self):
#Paths could came with any format, so first we will canonize them.
#i.e. make them absolute paths. Those method will return a path
#in it's absolute form or None if an absolute path cannot be formed.
#Canonize the paths...
self.__frame_path = self.__canonize_path(self.__frame_path);
self.__images_path = self.__canonize_path(self.__images_path);
self.__output_path = self.__canonize_path(self.__output_path);
#If any path cannot be formed fail now.
##Frame Path.
if(self.__frame_path is None or self.__frame_path == ""):
raise ValueError("Frame Path cannot be empty.");
elif(not os.path.isfile(self.__frame_path)):
raise ValueError("Frame Path is not a valid file",
ColorPath(self.__frame_path));
##Images Path.
if(self.__images_path is None or self.__images_path == ""):
raise ValueError("Images Dir Path cannot be empty.");
elif(not os.path.isdir(self.__images_path)):
raise ValueError("Images Dir Path is not a valid directory",
ColorPath(self.__images_path));
##Output Path.
if(self.__output_path is None or self.__images_path == ""):
raise ValueError("Output Dir Path cannot be empty.");
#An output path can not exists yet so create it now.
try:
os.makedirs(self.__output_path);
except OSError, e:
#Prevent the File Exists to be raised.
if(e.errno != os.errno.EEXIST):
e.filename = ColorPath(e.filename);
raise e;
#Here we have:
# 1 - The output directory already existed
# So os.makedirs failed but we prevent the exception
# to be raised, since it will raise in ALL existing dirs
# and we don't want this - BUT WE DON'T KNOW if we HAVE
# the write permission for this dir or not
# (it could be / for uid != 0)
# 2 - The output directory didn't existed but was
# successfully create - This case is the OK case
# since we create it we HAVE the write permission
# for the directory.
#
#So we MUST check if we HAVE the WRITE perms and if not fail now.
#doing this here will prevent the pygame.image.save to fail and
#it will be much more easier to user to check the problem
#and for us to report it.
if(not os.access(self.__output_path, os.W_OK)):
raise ValueError("Output Dir Path is not writable.",
ColorPath(self.__output_path));
def __canonize_path(self, path):
path = path.strip(" ");
if(path == ""):
return path;
return os.path.abspath(os.path.expanduser(path));
def __clean_images_filenames_list(self):
clean_filenames = [];
#Iterate for all files in the images directory and
#only selects those that is like to be an image.
#This not means that the file is a valid image, but it
#will be handled after.
filenames = os.listdir(self.__images_path);
for filename in filenames:
name, ext = os.path.splitext(filename);
if(ext in Constants.IMAGE_FORMATS):
clean_filenames.append(filename);
self.__images_filenames = clean_filenames;
def __merge_photo(self, input_filename, output_filename):
#Load both the Frame Image and the Image that will be merged.
#Merge them an then save the merged image into disk.
try:
frame_surface = pygame.image.load(self.__frame_path);
image_surface = pygame.image.load(input_filename);
image_surface.blit(frame_surface, (0,0));
pygame.image.save(image_surface, output_filename);
#If anything went wrong, so fail now.
#COWTODO: Change to a better exception.
except Exception, e:
msg = "{} ({}) - Exception: {}".format("Error while merging photo",
input_filename,
str(e))
raise Exception(msg);
################################################################################
## Color Stuff ##
################################################################################
ColorFatal = Color(RED);
ColorPath = Color(MAGENTA);
ColorNumber = Color(BLUE);
ColorCorrect = Color(GREEN);
ColorProcessing = Color(YELLOW);
################################################################################
## Print Functions ##
################################################################################
def print_help():
help = """Usage:
frame-merger [-hv]
frame-merger -f <frame-path> -i <images-path> -o <output-path> --jpg --no-colors
frame-merger --gui [-f <frame-path> -i <images-path> -o <output-path> --jpg]
Options:
*-h --help : Show this screen.
*-v --version : Show app version and copyright.
-f --frame <frame-path> : Path for the frame image.
-i --images-dir <images-path> : Path for dir of images that will be merged.
-o --output-path <output-path> : Path of the output dir.
--gui : Run in graphical mode. (If any other flags are given it will
loaded in graphical mode too).
--jpg : Force all output images to be jpg.
--no-colors : Disable the colored output.
Notes:
Options marked with * are exclusive, i.e. the frame-merger will run that
and exit successfully after the operation.
In command line mode the [-f -i -o] flags are required.
In GUI mode the [--no-colors] flag are ignored.
""";
print help;
exit(0);
def print_version():
print "{} - {} - {}".format(Constants.APP_NAME,
Constants.APP_VERSION,
Constants.APP_AUTHOR);
print Constants.APP_COPYRIGHT;
print;
exit(0);
def print_fatal(msg):
print ColorFatal("[FATAL]"), msg;
exit(1);
################################################################################
## Helper Functions ##
################################################################################
def run(frame_path, images_path, output_path, save_in_jpg):
#Merge all photos.
try:
merge_process = MergeProcess();
merge_process.set_frame_path (frame_path);
merge_process.set_images_path(images_path);
merge_process.set_output_path(output_path);
merge_process.set_save_in_jpg(save_in_jpg);
merge_process.init();
images_found = ColorNumber(merge_process.get_images_count());
print "Images found: ({})".format(images_found);
while(merge_process.has_image_to_merge()):
current_number = merge_process.get_current_image_index() + 1;
total_number = merge_process.get_images_count();
padding = len(str(total_number)) - len(str(current_number));
fmt = "{} ({}) {} ({})"
print fmt.format(ColorProcessing("Merging image"),
ColorNumber (("0" * padding) + str(current_number)),
ColorProcessing("of"),
ColorNumber (total_number)),
merge_process.merge();
print ColorCorrect("[OK]");
print ColorCorrect("Done...");
#COWTODO: Comment
except ValueError, e:
print_fatal(" - ".join(e.args));
except OSError, e:
print_fatal("Errno {} - {} - {}".format(e.errno,
e.strerror,
ColorPath(e.filename)));
################################################################################
## Script Initialization ##
################################################################################
def main():
#We don't want the None to be a string.
ConvertMode.mode = ConvertMode.NONE_TYPE_TO_EMPTY_STR;
if(len(sys.argv[1:]) == 0):
print_help();
#Get the command line options.
try:
options = getopt.gnu_getopt(sys.argv[1:],
Constants.ALL_FLAGS_SHORT,
Constants.ALL_FLAGS_LONG);
except Exception, e:
print_fatal(e);
#Vars to hold the values of the non exclusive command line options.
opt_frame_image_path = "";
opt_images_dir_path = "";
opt_output_dir_path = "";
opt_gui = False;
opt_save_in_jpg = False;
opt_nocolors = False;
#Parse the options.
for option in options[0]:
key, value = option;
key = key.lstrip("-");
#Help / Version / GUI. - EXCLUSIVE OPTIONS : Run and quit.
if (key in Constants.FLAG_HELP ): print_help();
elif(key in Constants.FLAG_VERSION ): print_version();
elif(key in Constants.FLAG_GUI ): opt_gui = True;
#Frame / Images / Output - MANDATORY OPTIONS : Grab the info to use later.
elif(key in Constants.FLAG_FRAME_IMAGE_PATH ): opt_frame_image_path = value;
elif(key in Constants.FLAG_IMAGES_DIR_PATH ): opt_images_dir_path = value;
elif(key in Constants.FLAG_OUTPUT_DIR_PATH ): opt_output_dir_path = value;
#JPG / No colors - OPTIONAL OPTIONS
elif(key in Constants.FLAG_FORCE_JPG): opt_save_in_jpg = True;
elif(key in Constants.FLAG_NO_COLORS): opt_nocolors = True;
#Setup the colors.
if(opt_nocolors):
ColorMode.mode = ColorMode.NEVER;
#Will run in GUI.
if(opt_gui):
GUI.run(opt_frame_image_path,
opt_images_dir_path,
opt_output_dir_path,
opt_save_in_jpg);
exit(0);
#Will run in text mode.
run(opt_frame_image_path,
opt_images_dir_path,
opt_output_dir_path,
opt_save_in_jpg);
################################################################################
## Script Initialization ##
################################################################################
if(__name__ == "__main__"):
main();