forked from Vdesnoux/Solex_ser_recon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInti_selector.py
631 lines (524 loc) · 18.6 KB
/
Inti_selector.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 19 18:24:02 2021
@author: valer
Version O.1
- ajout de erase graphique pour eviter les supperpositions d'images
- bug click dans list - oubli de graph move
- adaptation au nouveau log de exp par sharpcap
"""
import numpy as np
#import matplotlib.pyplot as plt
#from astropy.io import fits
import PySimpleGUI as sg
import os, fnmatch
import io
#import cv2
import sys
import yaml
from PIL import Image
import time
global mydic_files
current_version = ' Inti Selector V0.1 by V.Desnoux'
def read_camsetting (fich):
basefich=fich[1:9]+'.CameraSettings.txt'
d = {}
try:
with open(basefich) as f:
for line in f:
try:
(key, val) = line.split('=')
d[key] = val[:-1]
except:
pass
except:
print('file not found : ', basefich)
return d
def get_gainexp (fich):
# lexture fichier de camera setting
cam_dict=read_camsetting(fich)
try:
Gain=cam_dict['Gain']
# convert in ms
try:
Exp=float(cam_dict['Exposure'])*1000
Exp=str(float("{:.2f}".format(Exp)))+' ms'
except:
Exp=cam_dict['Exposure']
except:
Gain='error'
Exp='error'
cam_text=fich +' Gain: '+Gain+' Exp: '+Exp
return cam_text
def img_resize (nomfich,dim):
image = Image.open(nomfich)
image.thumbnail((dim, dim))
bio = io.BytesIO()
image.save(bio, format="PNG")
out=bio.getvalue()
#with io.BytesIO() as output:
#image.save(output, format="PNG")
#out = output.getvalue()
return out
"""
------------------------------------------------------------------------------
------------------------------------------------------------------------------
INTI Selector starts here
Permet la comparaison d'images png pour selectioner la meilleure
- image de reference à droite
- image de comparaison à gauche
- boutons pour zoomer et se deplacer dans l'image
- sauve dans fichier _log.txt les noms des fichiers qu'on logge
------------------------------------------------------------------------------
------------------------------------------------------------------------------
"""
# recupere les parametres utilisateurs enregistrés lors de la session
# precedente dans un fichier ini.yamp
#my_ini=os.path.dirname(sys.argv[0])+'/inti.yaml'
my_ini=os.getcwd()+'/inti.yaml'
my_dictini={'directory':'', 'dec doppler':3, 'dec cont':15, 'poly_slit_a':0, "poly_slit_b":0,'poly_slit_c':0, 'ang_tilt':0, 'ratio_sysx':0}
#print('myini de depart:', my_ini)
try:
#print('mon repertoire : ', mydir_ini)
with open(my_ini, "r") as f1:
my_dictini = yaml.safe_load(f1)
WorkDir=my_dictini['directory']
except:
WorkDir=''
"""
------------------------------------------------------------------------------
GUI fenetre appli
------------------------------------------------------------------------------
"""
sg.theme('Dark2')
sg.theme_button_color(('white', '#500000'))
colonne0 =[
[sg.Graph(canvas_size=(650,650),graph_bottom_left=(0, 0),graph_top_right=(650, 650),drag_submits=True, enable_events=True,key='-CROP0-')],
[sg.Text('', size=(40,1),key='-NOM0-')]
]
colonne1= [
[sg.Graph(canvas_size=(650,650),graph_bottom_left=(0, 0),graph_top_right=(650, 650),drag_submits=True, enable_events=True,key='-CROP1-')],
[sg.Text('', size=(40,1),key='-NOM1-')]
]
colonne_mid=[
[sg.InputText(visible=False, key='-OPENLIST-',enable_events=True),
sg.FilesBrowse('Open list',target='-OPENLIST-',initial_folder=WorkDir,file_types=(("time txt", "t*_log.txt"),("all", "*.txt")))],
[sg.Listbox(values='', select_mode='extended',size=(20,10), enable_events=True, key='-LIST-')],
[sg.Text('',size=(15,3))],
[sg.Text('',size=(15,3))],
[sg.Button ("Zoom In", size=(10,1))],
[sg.Button("Zoom Out", size=(10,1)),],
[sg.Text('',size=(15,3))],
[sg.Text('',size=(15,3))],
[sg.Button ("Reset", size=(10,1))]
]
layout = [
[sg.Text('Directory', size=(15, 1)), sg.InputText(default_text='',size=(100,1),enable_events=True,key='-FOLDER-'),
sg.FolderBrowse('Open',initial_folder=WorkDir),
sg.Combo(['*_clahe.png','*_protus.png', '*_diskHC.png','*_raw.png'],default_value='*_clahe.png',enable_events=True,key='-PAT-'),
sg.Text(current_version, size=(30, 1),text_color='Tan', font=("Arial", 8, "italic"))],
[sg.Column(colonne0), sg.Column(colonne_mid,element_justification='center'),sg.Column(colonne1)],
[sg.Button("Previous", size=(15,1)),sg.Button("Next", size=(15,1)),
sg.Button ("Keep", size=(15,1)),sg.Button("Log", size=(15,1)),
sg.Button ("Selected", size=(15,1)),
sg.Button ("Post Process", size=(25,1), visible=False),
sg.Button("Exit")]
]
window = sg.Window('INTI Selector', layout, finalize=True,location=(0,0), return_keyboard_events=True)
window['-FOLDER-'].update(WorkDir)
window.BringToFront()
"""
----------------------------------------------------------------------------
Initialisation des variables
----------------------------------------------------------------------------
"""
i=0 # indice du nom de fichier dans la liste
mylog=[] # liste qui contient les noms de fichiers que l'on log
nb_img=1000 #valeur max dans un repertoire
#coordonnées en haut à droite
x0=0
y0=650
#ecart position du graph
dx0=0
dy0=0
#dimension du canvas
dim0=650
dim=dim0
#ajoute ou retire 300 pixels pour resize image plus ou moins grande (zoom)
step_zoom=100
#deplacement du panning
step_move=100
# indice du fichier qui est dans l'emplacement de reference
indice_ref=0
# reference les zones images et la liste
graph0=window.Element("-CROP0-")
graph1=window.Element("-CROP1-")
sel_list=[]
#flag post_process
flag_postproc=False
if WorkDir !='':
# on charge le premier fichier dans les deux fenetres
os.chdir(WorkDir)
pattern='*_clahe.png'
mylog.append(pattern+'\n')
clahe_files=fnmatch.filter(os.listdir(WorkDir), pattern)
bio=img_resize(clahe_files[0],dim)
combo = get_gainexp(clahe_files[0])
window['-NOM0-'].update(combo)
window['-NOM1-'].update(combo)
graph0.DrawImage(data=bio, location=(x0,y0))
graph1.DrawImage(data=bio, location=(x0,y0))
nb_img=len(clahe_files)
acq_files=[acq_files[:10]+'log.txt' for acq_files in clahe_files]
# met a jour la listbox
window.Element('-LIST-').update(values=clahe_files)
# pour la souris
dragging = False
start_point = end_point = prior_rect = None
"""
-----------------------------------------------------------------------------
Boucle events de la window
-----------------------------------------------------------------------------
"""
while True:
event, values = window.read()
if event=='-PAT-':
i=0
dx0=0
dy0=0
x0=0
y0=650
indice_ref=0
dim=dim0
pattern=values['-PAT-']
mylog.append(pattern+'\n')
graph0.erase()
graph1.erase()
sel_list=[]
sel_indice=0
clahe_files=fnmatch.filter(os.listdir(WorkDir), pattern)
window.Element('-LIST-').update(values=clahe_files)
bio=img_resize(clahe_files[0],dim)
combo = get_gainexp(clahe_files[i])
window['-NOM0-'].update(combo)
window['-NOM1-'].update(combo)
graph0.DrawImage(data=bio, location=(x0,y0))
graph1.DrawImage(data=bio, location=(x0,y0))
nb_img=len(clahe_files)
if event=='Previous':
if i>0:
graph1.erase()
i=i-1
bio=img_resize(clahe_files[i],dim)
combo = get_gainexp(clahe_files[i])
window['-NOM1-'].update(combo)
graph1.DrawImage(data=bio, location=(x0,y0))
graph1.move(dx0,dy0)
if event=='Next':
if i<(nb_img-1):
graph1.erase()
i=i+1
bio=img_resize(clahe_files[i],dim)
combo = get_gainexp(clahe_files[i])
window['-NOM1-'].update(combo)
graph1.DrawImage(data=bio,location=(x0,y0))
graph1.move(dx0,dy0)
if event=="Keep":
graph0.erase()
bio=img_resize(clahe_files[i],dim)
# lexture fichier de camera setting
combo = get_gainexp(clahe_files[i])
window['-NOM0-'].update(combo)
graph0.DrawImage(data=bio, location=(x0,y0))
graph0.move(dx0,dy0)
indice_ref=i
# we log also
combo = get_gainexp(clahe_files[i])
mylog.append(combo +'\n')
sel_list.append(i)
window.Element('-LIST-').update(set_to_index=sel_list, scroll_to_index=i)
if event=="Zoom In" or event=='MouseWheel:Up':
if dim <=8000:
graph1.erase()
graph0.erase()
dim=dim+step_zoom
x0=x0-step_zoom/2
y0=y0+step_zoom/2
bio=img_resize(clahe_files[indice_ref],dim)
graph0.DrawImage(data=bio, location=(x0,y0))
graph0.move(dx0,dy0)
bio=img_resize(clahe_files[i],dim)
graph1.DrawImage(data=bio, location=(x0,y0))
graph1.move(dx0,dy0)
else:
print ('image plus grande que 8000 pixels')
if event=="Zoom Out" or event=='MouseWheel:Down':
if dim >=(650+step_zoom):
graph1.erase()
graph0.erase()
dim=dim-step_zoom
if dim>=650:
x0=x0+step_zoom/2
y0=y0-step_zoom/2
else:
x0=0
y0=650
graph0.erase()
graph1.erase()
bio=img_resize(clahe_files[indice_ref],dim)
graph0.DrawImage(data=bio, location=(x0,y0))
graph0.move(dx0,dy0)
bio=img_resize(clahe_files[i],dim)
graph1.DrawImage(data=bio, location=(x0,y0))
graph1.move(dx0,dy0)
else:
print ('image sera trop petite')
if event=="-FOLDER-":
WorkDir = values['-FOLDER-']
os.chdir(WorkDir)
pattern='*_clahe.png'
clahe_files=fnmatch.filter(os.listdir(WorkDir), pattern)
if len(clahe_files)!=0 :
bio=img_resize(clahe_files[0],dim)
combo = get_gainexp(clahe_files[0])
window['-NOM0-'].update(combo)
window['-NOM1-'].update(combo)
graph0.DrawImage(data=bio, location=(x0,y0))
graph1.DrawImage(data=bio, location=(x0,y0))
nb_img=len(clahe_files)
window.Element('-LIST-').update(values=clahe_files)
else:
print('no files found')
if event=='-OPENLIST-':
file_list=values['-OPENLIST-']
if file_list !='':
#print('fichier image selectionnées : '+file_list)
with open(file_list) as f:
read_lines=f.readlines()
n=[a.split(" ")[0] for a in read_lines]
b=[a for a in n if a[0]=="_" ]
clahe_files=b
if len(clahe_files)!=0 :
bio=img_resize(clahe_files[0],dim)
combo = get_gainexp(clahe_files[0])
window['-NOM0-'].update(combo)
window['-NOM1-'].update(combo)
graph0.DrawImage(data=bio, location=(x0,y0))
graph1.DrawImage(data=bio, location=(x0,y0))
nb_img=len(clahe_files)
window.Element('-LIST-').update(values=clahe_files)
if event=="Log":
combo = get_gainexp(clahe_files[i])
mylog.append(combo +'\n')
sel_list.append(i)
window.Element('-LIST-').update(set_to_index=sel_list, scroll_to_index=i)
if event=="Selected" and len(sel_list)!=0:
clahe_files=[clahe_files[k] for k in sel_list]
window.Element('-LIST-').update(values=clahe_files)
i=0
indice_ref=0
nb_img=len(clahe_files)
sel_list=[]
graph0.erase()
graph1.erase()
bio=img_resize(clahe_files[0],dim)
combo = get_gainexp(clahe_files[i])
window['-NOM0-'].update(combo)
window['-NOM1-'].update(combo)
graph0.DrawImage(data=bio, location=(x0,y0))
graph1.DrawImage(data=bio, location=(x0,y0))
# on sauve les noms de fichiers loggés
with open("t"+str(int(time.time()))+'_log.txt', "w") as logfile:
logfile.writelines(mylog)
mylog=[]
if event=="-LIST-" and len(values['-LIST-'])!=0 :
i=clahe_files.index(values['-LIST-'][len(values['-LIST-'])-1])
graph1.erase()
bio=img_resize(clahe_files[i],dim)
combo = get_gainexp(clahe_files[i])
window['-NOM1-'].update(combo)
graph1.DrawImage(data=bio, location=(x0,y0))
graph1.move(dx0,dy0)
if event==sg.WIN_CLOSED or event=='Exit':
break
if event=="Reset":
x0=0
y0=650
dim=dim0
dx0=dy0=0
graph0.erase()
graph1.erase()
bio=img_resize(clahe_files[i],dim)
graph1.DrawImage(data=bio, location=(x0,y0))
bio=img_resize(clahe_files[indice_ref],dim)
graph0.DrawImage(data=bio, location=(x0,y0))
if event=='-CROP1-' or event=='-CROP0-':
if event=='-CROP1-':
x,y=values['-CROP1-']
if event=='-CROP0-':
x,y=values['-CROP0-']
if not dragging:
start_point=(x,y)
dragging =True
Lastxy=x,y
else:
end_point=(x,y)
delta_x, delta_y= x-Lastxy[0], y-Lastxy[1]
graph1.move(delta_x, delta_y)
graph0.move(delta_x, delta_y)
Lastxy=x,y
if event.endswith('+UP'):
#print (event)
try:
dx0=dx0+(end_point[0]-start_point[0])
dy0=dy0+(end_point[1]-start_point[1])
except:
pass
start_point, end_point = None, None # enable grabbing a new rect
dragging = False
if event=="Post Process":
flag_postproc=True
file_to_proc=clahe_files[indice_ref]
print(file_to_proc)
break
window.close()
"""
-----------------------------------------------------------------------------
Sortie de l'application
-----------------------------------------------------------------------------
"""
#print (my_ini)
# on sauve les noms de fichiers loggés
if mylog[-1][0]!="*" :
with open("t"+str(int(time.time()))+'_log.txt', "w") as logfile:
logfile.writelines(mylog)
#met a jour le repertoire si on a changé dans le fichier ini.yaml
my_dictini['directory']=WorkDir
my_dictini['dec doppler']=3
my_dictini['dec cont']=15
try:
with open(my_ini, "w") as f1:
yaml.dump(my_dictini, f1, sort_keys=False)
except:
print('erreur ecriture inti.yaml')
if flag_postproc==False :
sys.exit()
sg.theme('Dark2')
sg.theme_button_color(('white', '#500000'))
colonne0 =[
[sg.Graph(canvas_size=(650,650),graph_bottom_left=(0, 0),graph_top_right=(650, 650),drag_submits=True, enable_events=True,key='-IMG-')],
[sg.Text('', size=(35,1),key='-TEXT-')]
]
colonne_droite=[
[sg.Slider(orientation ='vertical',enable_events=True, key='-SLIDC-', range=(1,10)),sg.Slider(orientation ='vertical', enable_events=True, key='-SLIDB-',range=(1,10))],
[sg.Text('',size=(15,3))],
[sg.Button ("Reset", size=(10,1))]
]
layout = [
[sg.Column(colonne0), sg.Column(colonne_droite,element_justification='center')],
[sg.Button("Save"), sg.Button("Exit")]
]
window = sg.Window('INTI processor', layout, finalize=True,location=(0,0), return_keyboard_events=True)
window.BringToFront()
#initialisation diverses
graph=window.Element("-IMG-")
bio=img_resize(file_to_proc,dim)
combo = get_gainexp(file_to_proc)
window['-TEXT-'].update(combo)
graph.DrawImage(data=bio, location=(x0,y0))
#coordonnées en haut à droite
x0=0
y0=650
#ecart position du graph
dx0=0
dy0=0
#dimension du canvas
dim0=650
dim=dim0
#ajoute ou retire 300 pixels pour resize image plus ou moins grande (zoom)
step_zoom=100
#deplacement du panning
step_move=100
# pour la souris
dragging = False
start_point = end_point = prior_rect = None
"""
-----------------------------------------------------------------------------
Boucle events de la window
-----------------------------------------------------------------------------
"""
while True:
event, values = window.read()
if event=='MouseWheel:Up':
if dim <=8000:
dim=dim+step_zoom
x0=x0-step_zoom/2
y0=y0+step_zoom/2
bio=img_resize(file_to_proc,dim)
graph.DrawImage(data=bio, location=(x0,y0))
graph.move(dx0,dy0)
else:
print ('image plus grande que 8000 pixels')
if event=='MouseWheel:Down':
if dim >=(650+step_zoom):
dim=dim-step_zoom
if dim>=650:
x0=x0+step_zoom/2
y0=y0-step_zoom/2
else:
x0=0
y0=650
graph.erase()
bio=img_resize(file_to_proc,dim)
graph.DrawImage(data=bio, location=(x0,y0))
graph.move(dx0,dy0)
else:
print ('image sera trop petite')
if event=='-IMG-':
x,y=values['-IMG-']
if not dragging:
start_point=(x,y)
dragging =True
Lastxy=x,y
else:
end_point=(x,y)
delta_x, delta_y= x-Lastxy[0], y-Lastxy[1]
graph.move(delta_x, delta_y)
Lastxy=x,y
if event.endswith('+UP'):
#print (event)
try:
dx0=dx0+(end_point[0]-start_point[0])
dy0=dy0+(end_point[1]-start_point[1])
except:
pass
start_point, end_point = None, None # enable grabbing a new rect
dragging = False
if event=="Reset":
x0=0
y0=650
dim=dim0
dx0=dy0=0
graph.erase()
bio=img_resize(file_to_proc,dim)
graph.DrawImage(data=bio, location=(x0,y0))
if event=="Save":
print ("on sauve la : ", WorkDir+"t"+file_to_proc)
if event==sg.WIN_CLOSED or event=='Exit':
break
if event=="-SLIDC-":
print("event slider C")
#read the image
im = Image.open(file_to_proc)
print(im.mode)
im_array=np.array(im)
print (im_array.shape, im_array.dtype)
factor = 1.5
#im = ImageEnhance.Contrast(im).enhance(factor)
im.thumbnail((dim, dim))
bio = io.BytesIO()
im.save(bio, format="PNG")
out=bio.getvalue()
graph.DrawImage(data=bio, location=(x0,y0))
window.close()
sys.exit()