-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyImageFuser.py
240 lines (219 loc) · 11.9 KB
/
PyImageFuser.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
# -*- coding: utf-8 -*-
# firmware_modder.py - This is the main script to modify the firmware
# for the AllappUpdate.bin
# Copyright (c) 2022, Harry van der Wolf. all rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public Licence as published
# by the Free Software Foundation, either version 2 of the Licence, or
# version 3 of the Licence, or (at your option) any later version. It is
# provided for educational purposes and 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 Licence for more details.
import PySimpleGUI as sg
import os, sys , tempfile, threading
# Necessary for windows
import requests
from pathlib import Path
import cv2
#------- Helper python scripts ----
import run_long_functions
import ui_layout
import Settings
import image_functions
import program_texts
import file_functions
#------- Some constants & variables -----------
sg.theme('SystemDefault1') # I hate all these colourful, childish themes. Please play with it and then grow up.
sg.SetOptions(font = ('Helvetica', 12))
filenames = []
pathnames = []
images = [] # This one is used for our calculations
resized_images = [] # This one contains the complete path names of our resized images
full_images = [] # this one contains the complete path names of the original images
image_exif_dictionaries = {}
image_formats = (('image formats', '*.jpg *.JPG *.jpeg *.JPEG *.png *.PNG *.tif *.TIF *.tiff *.TIFF'),)
#thread_done = 1
#----------------------------------------------------------------------------------------------
#------------------------------- Helper functions ---------------------------------------------
# This functions
def disable_elements(window, disable_elements):
'''
This function disables / enables some buttons on the screen
and does the opposite action for the output element
:param window: main window where elements are acted upon
:type window: Window
:param disable_elements: True or False
:type disable_elements: (bool)
'''
if disable_elements:
window['_btnLoadImages_'].update(disabled=True)
window['_btnPreferences_'].update(disabled=True)
window['_select_all_'].update(disabled=True)
window['_create_preview_'].update(disabled=True)
window['_CreateImage_'].update(disabled=True)
window['_noise_reduction_'].update(disabled=True)
window['_Close_'].update(disabled=True)
window['_sgOutput_'].update(visible=True) # When buttons are disabled, output window becomes visible
else:
window['_btnLoadImages_'].update(disabled=False)
window['_btnPreferences_'].update(disabled=False)
window['_select_all_'].update(disabled=False)
window['_create_preview_'].update(disabled=False)
window['_CreateImage_'].update(disabled=False)
window['_noise_reduction_'].update(disabled=False)
window['_Close_'].update(disabled=False)
window['_sgOutput_'].update(visible=False) # When buttons are enabled, output window is hidden
def replace_strings(Lines, orgstring, newstring):
newLines = []
# Now do the replacing
for line in Lines:
if orgstring not in line:
newLines.append(line)
else:
newLines.append(newstring + '\n')
return newLines
"""
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- Everything else
"""
def logger(e):
e = sys.exc_info()
print('Error Return Type: ', type(e))
print('Error Class: ', e[0])
print('Error Message: ', e[1])
return str(e[1])
#----------------------------------------------------------------------------------------------
#----------------------------- Main function --------------------------------------------------
def main():
#global thread_done
tmpfolder = os.path.join(os.path.realpath(tempfile.gettempdir()), 'pyimagefuser')
settingsFile = os.path.join(os.path.realpath(Path.home()), '.pyimagefuser.json')
file_functions.recreate_tmp_workfolder(tmpfolder)
sg.user_settings_filename(path=os.path.realpath(Path.home()))
start_folder = sg.user_settings_get_entry('imgfolder', os.path.realpath(Path.home()))
print("\ntmpfolder: ", tmpfolder)
print("settingsfile: ",settingsFile)
print("tmpdir", os.path.realpath(tempfile.gettempdir()))
print("current path ", os.path.realpath('.'))
# Display the GUI to the user
window = ui_layout.create_and_show_gui(tmpfolder,start_folder)
while True:
event, values = window.Read(timeout=100)
if event == sg.WIN_CLOSED or event == '_Close_' or event == 'Exit':
file_functions.remove_tmp_workfolder(tmpfolder)
#print('pressed Close')
return('Cancel', values)
break
elif event == '-FILES-':
filenames = []
window['-FILE LIST-'].update(filenames)
if values["-FILES-"]: # or values["-FILES-"] == {}: #empty list returns False
file_list = values["-FILES-"].split(";")
for file in file_list:
# print(f)
fname = os.path.basename(file)
folder = os.path.dirname(os.path.abspath(file))
# Now save this folder as "last opened folder" to our settings
sg.user_settings_filename(path=Path.home())
sg.user_settings_set_entry('last_opened_folder', folder)
filenames.append(fname)
pathnames.append(file)
# get all exif date if available
image_exif_dictionaries[fname] = image_functions.get_all_exif_info(file)
window['-FILE LIST-'].update(filenames)
window['-FOLDER-'].update(folder)
elif event == 'About...':
window.disappear()
sg.popup(program_texts.about_message, grab_anywhere=True, keep_on_top=True, icon=image_functions.get_icon())
window.reappear()
elif event == 'Credits':
window.disappear()
sg.popup(program_texts.Credits, grab_anywhere=True, keep_on_top=True, icon=image_functions.get_icon())
window.reappear()
elif event == 'Preferences' or event =='_btnPreferences_':
Settings.settings_window()
elif event == '_select_all_':
print('_select_all_')
#print(str(values["-FILES-"].split(";")))
#index = range(len(values["-FILES-"].split(";")))
#window["-FILE LIST-"](set_to_index=[index], scroll_to_index=index)
elif event == "-FILE LIST-": # A file was chosen from the listbox
#print('A file was chosen from the listbox')
if values['_display_selected_'] and len(values['-FILE LIST-']) !=0:
image_functions.resizesingletopreview(folder, tmpfolder, values['-FILE LIST-'][0])
image_functions.display_preview(window, os.path.join(tmpfolder, values['-FILE LIST-'][0]))
elif event == '_create_preview_':
# The exposure fusion and aligning has been copied from below page
# https://learnopencv.com/exposure-fusion-using-opencv-cpp-python/
print('User pressed Create Preview\n')
if len(values['-FILE LIST-']) >1: # We have at least 2 files
failed, resized_images = image_functions.resizetopreview(values, folder, tmpfolder)
print(failed)
go_on = False
if failed != '':
is_zero = file_functions.getFileSizes(values, tmpfolder)
if is_zero > 0:
#sg.popup_scrolled(program_texts.resize_error_message, failed, icon=image_functions.get_icon())
sg.popup(program_texts.resize_error_message, icon=image_functions.get_icon())
go_on = False # not necessary but do it anyway
else:
go_on = True
#sg.popup_scrolled(program_texts.resize_warning_message, failed, icon=image_functions.get_icon())
program_texts.popup_text_scroll('Something went wrong',program_texts.resize_warning_message, failed)
else: # failed = ''
go_on = True
if go_on:
if (values['_align_images_']):
aligned_images = image_functions.do_align_and_noise_reduction(resized_images, '', '', values, tmpfolder)
image_functions.exposure_fuse(values, aligned_images, tmpfolder, 'preview')
image_functions.display_preview(window, os.path.join(tmpfolder, 'preview.jpg'))
else: # Create preview without aligning
image_functions.align_fuse(values, resized_images, tmpfolder, 'preview', False) # False for "don't align"
image_functions.display_preview(window, os.path.join(tmpfolder, 'preview.jpg'))
else: # 1 or 0 images selected
sg.popup("You need to select at least 2 images", icon=image_functions.get_icon())
elif event == '_CreateImage_':
# The exposure fusion and aligning has been copied from below page
# https://learnopencv.com/exposure-fusion-using-opencv-cpp-python/
print('User pressed "Create Exposure fused image"\n')
if len(values['-FILE LIST-']) > 1: # We have at least 2 files
newFileName, full_images = image_functions.get_filename_images(values, folder)
if newFileName != '' and newFileName != 'Cancel':
disable_elements(window, True)
window.refresh()
if values['_align_images_']:
aligned_images = image_functions.do_align_and_noise_reduction(full_images, '', '', values, tmpfolder)
image_functions.exposure_fuse(values, aligned_images, tmpfolder, os.path.join(folder, newFileName))
else: # Create full image without aligning
#image_functions.align_fuse(values, full_images, tmpfolder, os.path.join(folder, newFileName),True) # True for "do align"
image_functions.exposure_fuse(values, full_images, tmpfolder, os.path.join(folder, newFileName))
disable_elements(window, False)
window.refresh()
if values['_dispFinalIMG_']:
image_functions.displayImageWindow(os.path.join(folder, newFileName))
else: # 1 or 0 images selected
sg.popup("You need to select at least 2 images", icon=image_functions.get_icon())
elif event == '_noise_reduction_':
# This code is copied from: https://github.com/maitek/image_stacking
print('User pressed "Create noise reduced image"\n')
if len(values['-FILE LIST-']) > 1: # We have at least 2 files
newFileName, full_images = image_functions.get_filename_images(values, folder)
if newFileName != '' and newFileName != 'Cancel':
disable_elements(window, True)
image_functions.do_align_noise_reduction(full_images, folder, newFileName, values, '')
disable_elements(window, False)
if values['_dispFinalIMG_']:
image_functions.displayImageWindow(os.path.join(folder, newFileName))
else: # 1 or 0 images selected
sg.popup("You need to select at least 2 images", icon=image_functions.get_icon())
window.Close()
#------------------- Main "boilerplate" function ----------------------------------------------
#----------------------------------------------------------------------------------------------
## Main program, main module
if __name__ == '__main__':
main()