-
Notifications
You must be signed in to change notification settings - Fork 0
/
gk_film_qa.py
executable file
·948 lines (746 loc) · 29.7 KB
/
gk_film_qa.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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
#!/usr/bin/env python3
# =============================================================================
# <one line to give the program's name and a brief idea of what it does.>
#
# Copyright (C) <yyyy> <Author Name> <author@mail.com>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
#
# =============================================================================
# =============================================================================
#
# <Put documentation here>
#
# <yyyy>-<mm>-<dd> <Author Name> <author@mail.com>
#
# * <programfilename>.py: created.
#
# =============================================================================
import argparse
import numpy as np
import tkinter as tk
import tkinter.ttk as ttk
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from PIL import Image
from enum import Enum
from imghdr import what
from os.path import basename
from sys import float_info as fi
from matplotlib import (cbook, use)
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg,
NavigationToolbar2Tk
)
use("TkAgg")
plt.style.use('bmh')
# =============================================================================
# Global constants
# =============================================================================
# Centimeters per inch.
CM_PER_IN = 2.54
MIN_FLOAT = fi.min
MAX_FLOAT = fi.max
# =============================================================================
# Utility classes and functions
# =============================================================================
class ImageFormats(Enum):
"""Class to wrap up enumerated values that define supoported image formats.
"""
png = 'png'
tiff = 'tiff'
class DisplayData(Enum):
"""Class to wrap up enumerated values that describe what image data to
be displayed.
"""
original = 0
red = 1
green = 2
blue = 3
class ProgramAction(object):
"""Abstract base class for all program actions, that provides execute.
The execute method contains code that will actually be executed after
arguments parsing is finished. The method is called from within method
run of the CommandLineApp instance.
"""
def __init__(self, exitf):
self._exit_app = exitf
def execute(self):
pass
def points_to_centimeters(dpi, this_many_points):
"""Utility function to convert from pixels (points) to centimeters
according to given dpi (dots per inch).
"""
# Set deafult return value. Default is negative value that is used to
# indicate missing or false input values.
returnval = -1
# Do sam basic sanity checks first.
if 0 < dpi and 0 <= this_many_points:
returnval = (this_many_points / dpi) * CM_PER_IN
return returnval
def _format_epilog(epilogAddition, bugMail):
"""Formatter for generating help epilogue text. Help epilogue text is an
additional description of the program that is displayed after the
description of the arguments. Usually it consists only of line informing
to which email address to report bugs to, or it can be completely
omitted.
Depending on provided parameters function will properly format epilogue
text and return string containing formatted text. If none of the
parameters are supplied the function will return None which is default
value for epilog parameter when constructing parser object.
"""
fmtMail = None
fmtEpilog = None
if epilogAddition is None and bugMail is None:
return None
if bugMail is not None:
fmtMail = 'Report bugs to <{bugMail}>.'\
.format(bugMail=bugMail)
else:
fmtMail = None
if epilogAddition is None:
fmtEpilog = fmtMail
elif fmtMail is None:
fmtEpilog = epilogAddition
else:
fmtEpilog = '{addition}\n\n{mail}'\
.format(addition=epilogAddition, mail=fmtMail)
return fmtEpilog
def _formulate_action(Action, **kwargs):
"""Factory method to create and return proper action object.
"""
return Action(**kwargs)
def _is_image_format_supported(filename):
"""Test if file is one of the image formats supported by application.
Supported image formats are defined by enumerated class ImageFormats at the
beginning of this script.
"""
image_type = what(filename)
if image_type:
try:
ImageFormats(image_type)
return True
except ValueError:
# We just want to stop exception to propagate further up. Nothing
# to do here actually, so we just pass.
pass
return False
# =============================================================================
# Command line app class
# =============================================================================
class CommandLineApp(object):
"""Actual command line app object containing all relevant application
information (NAME, VERSION, DESCRIPTION, ...) and which instantiates
action that will be executed depending on the user input from
command line.
"""
def __init__(
self,
programName=None,
programDescription=None,
programLicense=None,
versionString=None,
yearString=None,
authorName=None,
authorMail=None,
epilog=None):
self.programLicense = programLicense
self.versionString = versionString
self.yearString = yearString
self.authorName = authorName
self.authorMail = authorMail
fmtEpilog = _format_epilog(epilog, authorMail)
self._parser = argparse.ArgumentParser(
prog=programName,
description=programDescription,
epilog=fmtEpilog,
formatter_class=argparse.RawDescriptionHelpFormatter
)
# Since we add argument options to groups by calling group
# method add_argument, we have to sore all that group objects
# somewhere before adding arguments. Since we want to store all
# application relevant data in our application object we use
# this list for that purpose.
self._argumentGroups = []
@property
def programName(self):
"""Utility function that makes accessing program name attribute
neat and hides implementation details.
"""
return self._parser.prog
@property
def programDescription(self):
"""Utility function that makes accessing program description
attribute neat and hides implementation details.
"""
return self._parser.description
def add_argument_group(self, title=None, description=None):
"""Adds an argument group to application object.
At least group title must be provided or method will rise
NameError exception. This is to prevent creation of titleless
and descriptionless argument groups. Although this is allowed bu
argparse module I don't see any use of a such utility."""
if title is None:
raise NameError('Missing arguments group title.')
group = self._parser.add_argument_group(title, description)
self._argumentGroups.append(group)
return group
def _group_by_title(self, title):
group = None
for item in self._argumentGroups:
if title == item.title:
group = item
break
return group
def add_argument(self, *args, **kwargs):
"""Wrapper for add_argument methods of argparse module. If
parameter group is supplied with valid group name, argument will
be added to that group. If group parameter is omitted argument
will be added to parser object. In a case of invalid group name
it rises ValueError exception.
"""
if 'group' not in kwargs or kwargs['group'] is None:
self._parser.add_argument(*args, **kwargs)
else:
group = self._group_by_title(kwargs['group'])
if group is None:
raise ValueError(
'Trying to reference nonexisten argument group.'
)
else:
kwargsr = {k: kwargs[k] for k in kwargs.keys() if 'group' != k}
group.add_argument(*args, **kwargsr)
def parse_args(self, args=None, namespace=None):
"""Wrapper for parse_args method of a parser object. It also
instantiates action object that will be executed based on a
input from command line.
"""
arguments = self._parser.parse_args(args, namespace)
if arguments.usage:
self._action = _formulate_action(
ProgramUsageAction,
parser=self._parser,
exitf=self._parser.exit)
elif arguments.version:
self._action = _formulate_action(
ShowVersionAction,
prog=self._parser.prog,
ver=self.versionString,
year=self.yearString,
author=self.authorName,
license=self.programLicense,
exitf=self._parser.exit)
else:
filelist = (arguments.iradimage, arguments.preiradimage)
self._action = _formulate_action(
DefaultAction,
prog=self._parser.prog,
exitf=self._parser.exit,
filelist=filelist)
def run(self):
"""This method executes action code.
"""
self._action.execute()
# =============================================================================
# GUI classes
# =============================================================================
class TkInputFloat(tk.Frame):
""" Custom widget to collect user input of float values.
"""
def __init__(self, *args, **kwargs):
# Following arguments we use locally and rest we send to superclass:
# label: Label for input field. Text displayed above
# entry widget;
# buttontext: Text displayed on control button explaining
# command to be executed;
# bottomlimit: Bottom limit of possible values that can be entered.
# Default value is set to MIN_FLOAT;
# toplimit: Top limit of possible values that can be entered.
# Default value is set to MAX_FLOAT;
# command: A callback method for pasing input values.
# Default value is None.
label = None
buttontext = None
if 'label' in kwargs:
label = kwargs.pop('label')
else:
label = 'Float:'
if 'buttontext' in kwargs:
buttontext = kwargs.pop('buttontext')
else:
buttontext = 'Input'
if 'bottomlimit' in kwargs:
self._bottom = kwargs.pop('bottomlimit')
else:
self._bottom = MIN_FLOAT
if 'toplimit' in kwargs:
self._top = kwargs.pop('toplimit')
else:
self._top = MAX_FLOAT
if 'command' in kwargs:
self._command = kwargs.pop('command')
else:
self._command = None
# Pass the rest of arguments to superclass.
# tk.Frame.__init__(self, kwargs, className='TkInputFloat')
tk.Frame.__init__(self, *args, **kwargs)
# Initialize and arrange elemnts on the frame.
tk.Label(self, text=label, anchor='w').pack(side=tk.TOP, fill=tk.X)
# Frame to group and align entry field and command button.
entry_group = ttk.Frame(self)
# Set variable to keep track of input values.
self._str_val = tk.StringVar()
tk.Entry(entry_group, width=12, textvariable=self._str_val)\
.pack(side=tk.LEFT, fill=tk.Y, padx=1, pady=1)
tk.Button(entry_group, text=buttontext, command=self._button_pressed)\
.pack(side=tk.RIGHT, fill=tk.Y)
entry_group.pack(side=tk.BOTTOM, fill=tk.X)
def _button_pressed(self):
val = 0.0
# Try to convert string value to float.
try:
val = float(self._str_val.get())
except ValueError:
# We just ignore values that are not of float type.
pass
# We only accept values in range [self._bottom, self._top].
if self._command:
if val < self._bottom or val > self._top:
# Out of range so rest to initial value.
val = 0.0
self._command(val)
# Reset entry value.
self._str_val.set('')
class GKFilmQANavigationToolbar(NavigationToolbar2Tk):
""" TODO: Add class description.
"""
def __init__(self, canvas, window, image_dpi=None):
self.canvas = canvas
self.window = window
self._image_dpi = image_dpi
NavigationToolbar2Tk.__init__(self, canvas, window)
def mouse_move(self, event):
self._set_cursor(event)
if event.inaxes and event.inaxes.get_navigate():
try:
xdata = event.xdata
ydata = event.ydata
x = int(xdata)
y = int(ydata)
if self._image_dpi:
xcm = points_to_centimeters(
self._image_dpi[0],
float(xdata)
)
ycm = points_to_centimeters(
self._image_dpi[1],
float(ydata)
)
s = 'x: {0:.2f} cm [{1:d} pxs], y: {2:.2f} cm [{3:d} pxs],\
value: '.format(xcm, x, ycm, y)
else:
s = 'x: {0:d}, y: {1:d}, value: '.format(x, y)
except (ValueError, OverflowError):
pass
else:
artists = [a for a in event.inaxes._mouseover_set
if a.contains(event) and a.get_visible()]
if artists:
a = cbook._topmost_artist(artists)
if a is not event.inaxes.patch:
data = a.get_cursor_data(event)
if data is not None:
data_str = a.format_cursor_data(data)
if data_str is not None:
s = s + ' ' + data_str
if len(self.mode):
self.set_message('%s, %s' % (self.mode, s))
else:
self.set_message(s)
else:
self.set_message(self.mode)
class ImageRenderer(object):
""" A class that actually does the drawing of the image view.
"""
def __init__(self, figure, axes, imagedata, what):
self._figure = figure
self._axes = axes
self._imagedata = [imagedata]
self._what = what
def _update(self):
self._axes.clear()
title = None
cmap = None
displaydata = None
if DisplayData.red == self._what:
title = 'Red channel'
cmap = cm.gray
displaydata = np.asarray(self._imagedata[-1].getchannel('R'))
elif DisplayData.green == self._what:
title = 'Green channel'
cmap = cm.gray
displaydata = np.asarray(self._imagedata[-1].getchannel('G'))
elif DisplayData.blue == self._what:
title = 'Blue channel'
cmap = cm.gray
displaydata = np.asarray(self._imagedata[-1].getchannel('B'))
else:
title = 'Original'
displaydata = self._imagedata[-1]
# Try to determine image dpi.
image_dpi = None
try:
image_dpi = self._imagedata[-1].info['dpi']
except KeyError:
# Image info does not contain dpi key so do nothing.
pass
# Set default axes units.
units_str = '[px]'
# Try to set proper scale for axes (in centimeters), if image data
# supplied.
if image_dpi:
units_str = '[cm]'
title = '{0} [dpi: {1}]'.format(title, image_dpi)
ticks_x = ticker.FuncFormatter(lambda x, pos: '{0:.2f}'.format(
points_to_centimeters(image_dpi[0], float(x))
))
self._axes.xaxis.set_major_formatter(ticks_x)
ticks_y = ticker.FuncFormatter(lambda y, pos: '{0:.2f}'.format(
points_to_centimeters(image_dpi[1], float(y))
))
self._axes.yaxis.set_major_formatter(ticks_y)
# Set title.
self._axes.set_title(title)
# Set units label.
self._axes.set_xlabel(units_str)
self._axes.set_ylabel(units_str)
# Show plot.
self._axes.imshow(
displaydata,
cmap=cmap
)
self._figure.canvas.draw()
def update(self):
self._update()
def rotate_image(self, rotation_angle):
self._imagedata.append(self._imagedata[-1].rotate(
angle=-rotation_angle, # negative sign to rotate clockwise
resample=Image.NEAREST,
expand=True,
fillcolor='white'
))
self._update()
print('Image stack size: {0}'.format(len(self._imagedata)))
def undo_rotation(self):
if 1 < len(self._imagedata):
self._imagedata.pop()
self._update()
print('Image stack size: {0}'.format(len(self._imagedata)))
def toggle_channel(self, what):
self._what = what
self._update()
class ImageView(object):
""" A class used to hold and keep track of figure responsible for image
display and canvas that figure is drawn on.
"""
def __init__(self, master, image_dpi=None):
self._dpi = image_dpi
# self._figure = plt.Figure(dpi=72)
self._figure = plt.Figure()
FigureCanvasTkAgg(self._figure, master)
self._figure.canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True)
self._figure.canvas.draw()
# Initialize axes.
self._axes = self._figure.add_subplot(111)
# Add toolbar to each view so user can zoom, take screenshots, etc.
self._toolbar = GKFilmQANavigationToolbar(
self._figure.canvas,
master,
self._dpi
)
# Update toolbar display.
self._toolbar.update()
@property
def figure(self):
return self._figure
@property
def axes(self):
return self._axes
class GKFilmQAMainScreen(tk.Tk):
""" Application's main screen.
"""
def __init__(self, program_name, iraddata, preiraddata):
tk.Tk.__init__(self, className='GKFilmQAMainScreen')
self._program_name = program_name
# Set app icon, window title and make window nonresizable.
# tk.Tk.iconbitmap(self, default='dosxyz_show.ico')
self.title(basename(iraddata.filename))
# self.resizable(False, False)
self.resizable(False, False)
# Split top frame into two main frames. One for displaying image
# and other for controling display options.
# Set up view frame.
viewframe = ttk.LabelFrame(self, text='View')
view = ttk.Frame(viewframe, borderwidth=3)
view.pack(side=tk.TOP, fill=tk.X)
viewframe.pack(side=tk.LEFT, fill=tk.Y, padx=5, pady=5)
# Print some info to the command line.
print('{0}: Loading image data ...'.format(self._program_name))
# Try to determine image dpi.
image_dpi = None
try:
image_dpi = iraddata.info['dpi']
except KeyError:
# Image info does not contain dpi key so do nothing.
pass
# Connect view manager for the image frame.
self._imageview = ImageView(view, image_dpi=image_dpi)
self._imagerenderer = ImageRenderer(
self._imageview.figure,
self._imageview.axes,
iraddata,
DisplayData.original
)
# Set up control frame.
controlframe = ttk.LabelFrame(self, text='Controls')
# Split control frame into upper and lower half. Upper one is to hold
# actual display controls, while lower one holds 'Quit' button only.
topcontrol = ttk.Frame(controlframe)
# Set channel selection controls.
self._current_view = tk.IntVar()
self._btnoriginal = ttk.Radiobutton(
topcontrol,
text='Original image',
command=self._select_channel,
value=DisplayData.original.value,
variable=self._current_view
)
self._btnoriginal.pack(side=tk.TOP, fill=tk.X)
self._btnred = ttk.Radiobutton(
topcontrol,
text='Red channel',
command=self._select_channel,
value=DisplayData.red.value,
variable=self._current_view
)
self._btnred.pack(side=tk.TOP, fill=tk.X)
self._btngreen = ttk.Radiobutton(
topcontrol,
text='Green channel',
command=self._select_channel,
value=DisplayData.green.value,
variable=self._current_view
)
self._btngreen.pack(side=tk.TOP, fill=tk.X)
self._btnblue = ttk.Radiobutton(
topcontrol,
text='Blue channel',
command=self._select_channel,
value=DisplayData.blue.value,
variable=self._current_view
)
self._btnblue.pack(side=tk.TOP, fill=tk.X)
# Set default channel.
self._current_view.set(DisplayData.original.value)
# Set image rotation control.
TkInputFloat(
topcontrol,
label='Image rotation:',
buttontext='Rotate',
bottomlimit=-359.0,
toplimit=359.0,
command=self._rotate_image
).pack(side=tk.TOP, fill=tk.X)
# Set undo button.
ttk.Button(
topcontrol,
text='Undo rotation',
command=self._undo_rotation
).pack(side=tk.TOP, fill=tk.X)
topcontrol.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)
spacer = ttk.Frame(controlframe)
spacer.pack(side=tk.TOP, fill=tk.Y, expand=True)
# Set appllication "Quit" button.
bottomcontrol = ttk.Frame(controlframe)
ttk.Button(bottomcontrol, text='Quit', command=self.destroy)\
.pack(side=tk.TOP, fill=tk.X)
bottomcontrol.pack(side=tk.BOTTOM, fill=tk.X, padx=5, pady=5)
controlframe.pack(side=tk.RIGHT, fill=tk.Y, padx=5, pady=5)
# Update display.
self.update()
def _select_channel(self):
"""Method to be called when one of channel selection buttons is
checked. It invokes actual method that turns channel display on/off.
"""
what = None
if DisplayData.red.value == self._current_view.get():
what = DisplayData.red
elif DisplayData.green.value == self._current_view.get():
what = DisplayData.green
elif DisplayData.blue.value == self._current_view.get():
what = DisplayData.blue
else:
what = DisplayData.original
self._imagerenderer.toggle_channel(what)
def _rotate_image(self, angle):
"""A callback method for "Image rotation" control.
"""
if angle:
self._imagerenderer.rotate_image(angle)
def _undo_rotation(self):
"""A callback method for "Undo rotation" control.
"""
self._imagerenderer.undo_rotation()
def update(self):
"""Method to update diplay of main screen.
"""
self._imagerenderer.update()
# =============================================================================
# App action classes
# =============================================================================
class ProgramUsageAction(ProgramAction):
"""Program action that formats and displays usage message to the stdout.
"""
def __init__(self, parser, exitf):
self._usageMessage = \
'{usage}Try \'{prog} --help\' for more information.'\
.format(usage=parser.format_usage(), prog=parser.prog)
self._exit_app = exitf
def execute(self):
print(self._usageMessage)
self._exit_app()
class ShowVersionAction(ProgramAction):
"""Program action that formats and displays program version information
to the stdout.
"""
def __init__(self, prog, ver, year, author, license, exitf):
self._versionMessage = \
'{0} {1} Copyright (C) {2} {3}\n{4}'\
.format(prog, ver, year, author, license)
self._exit_app = exitf
def execute(self):
print(self._versionMessage)
self._exit_app()
class DefaultAction(ProgramAction):
"""Program action that wraps some specific code to be executed based on
command line input. In this particular case it prints simple message
to the stdout.
"""
def __init__(self, prog, exitf, filelist):
self._programName = prog
self._exit_app = exitf
self._filelist = filelist
def execute(self):
# Do some basic sanity checks first.
if self._filelist[0] is None:
print('{0}: Missing image file.'
.format(self._programName))
self._exit_app()
if not _is_image_format_supported(self._filelist[0]):
print(
'{0}: File \'{1}\' is not of supported image format.'
.format(self._programName, self._filelist[0])
)
self._exit_app()
if self._filelist[1] is not None:
if not _is_image_format_supported(self._filelist[1]):
print(
'{0}: File \'{1}\' is not of supported image format.'
.format(self._programName, self._filelist[1])
)
self._exit_app()
# We have a proper iradiated image file. Load the image data.
iraddata = Image.open(self._filelist[0])
# print('Image file: {0}'.format(iraddata.filename))
# print('Image format: {0}'.format(iraddata.format))
# print('Image mode: {0}'.format(iraddata.mode))
# print('Resolution (dpi): {0} x {1}'.format(
# iraddata.info['dpi'][0],
# iraddata.info['dpi'][1]
# ))
# print('Image size (px): {0} x {1}'.format(
# iraddata.width,
# iraddata.height
# ))
# print('Image size (cm): {0} x {1}'.format(
# (iraddata.width / iraddata.info['dpi'][0]) * CM_PER_IN,
# (iraddata.height / iraddata.info['dpi'][1]) * CM_PER_IN,
# ))
# print()
if self._filelist[1] is not None:
# We have proper preiradiated image file. Load it too.
preiraddata = Image.open(self._filelist[1])
else:
preiraddata = None
# Print some info to the command line.
print('{0}: Starting GUI ...'.format(self._programName))
# We have all neccessary files. Start the GUI.
mainscreen = GKFilmQAMainScreen(
program_name=self._programName,
iraddata=iraddata,
preiraddata=preiraddata
)
mainscreen.mainloop()
# Print to command line that we are freeing memory and closing app.
print('{0}: Freeing allocated memory ...'.format(self._programName))
# Do the cleanup and exit application.
iraddata.close()
if preiraddata is not None:
preiraddata.close()
self._exit_app()
# =============================================================================
# Script main body
# =============================================================================
if __name__ == '__main__':
description = 'Framework for application development \
implementing argp option parsing engine.\n\n\
Mandatory arguments to long options are mandatory for \
short options too.'
license = 'License GPLv3+: GNU GPL version 3 or later \
<http://gnu.org/licenses/gpl.html>\n\
This is free software: you are free to change and \
redistribute it.\n\
There is NO WARRANTY, to the extent permitted by law.'
program = CommandLineApp(
programDescription=description.replace('\t', ''),
programLicense=license.replace('\t', ''),
versionString='i.i',
yearString='yyyy',
authorName='Author Name',
authorMail='author@mail.com',
epilog=None)
program.add_argument_group('general options')
program.add_argument(
'-V',
'--version',
action='store_true',
help='print program version',
group='general options')
program.add_argument(
'--usage',
action='store_true',
help='give a short usage message')
program.add_argument(
'iradimage',
metavar='IRADIMAGE',
type=str,
nargs='?',
help='image of a scanned iradiated gafchromic film')
program.add_argument(
'preiradimage',
metavar='PREIRADIMAGE',
type=str,
nargs='?',
help='image of a scanned gafchromic film pre irradiation')
program.parse_args()
program.run()