-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopout3d.py
executable file
·1896 lines (1571 loc) · 74.5 KB
/
popout3d.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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/python3
'''
--------------------------------------------------------------------------------
GNU GENERAL PUBLIC LICENSE GPLv3
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 a copy of the GNU General Public License
in /usr/share/common-licenses/GPL-3. If not,
see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------------
'''
# Also depends on hugin_tools for align_image_stack.
import sys, os, shutil, shlex, glob, subprocess, multiprocessing
import warnings #43 to suppress GTK deprecation warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
from PIL import Image, ImageOps, __version__
from PIL.ExifTags import TAGS
try:
import gi
except:
sys.exit('Failed to import gi')
try:
gi.require_version('Gtk', '4.0')
except:
print(gi.__version__)
sys.exit('gi wrong version')
from gi.repository import Gtk, GdkPixbuf, Gio
try:
import gettext
except:
sys.exit('cannot import gettext')
try:
import locale
except:
print('Cannot import locale')
try:
import webbrowser
except:
print('Cannot import webbrowser')
#-------------------------------------------------------------------------------
# create global variables and set default values
version = '1.6.43a' # formatted for "About"
firstrun = True
viewDim = 'All' # which sort of images to show
viewType = 'ASCNP' # which type of 3D images to show
viewlist = [] # list of images to view
viewind = 0 # index of image to view
infolist = ''
dummyfile = 'dummy.png' # grey image to display when one is missing.
blankfile = 'blank.png' # blank image to display for clearing.
settingsfile = 'popout3d.dat' # settings file
logofile = 'popout3d.png' # logo for About
myfile = '' # current file
myext = '' # current extension
# myfold mustn't be defined
scope = 'Folder' # whether dealing with file set or folder
formatcode = 'A' # first letter of format
stylecode = 'N' # first letter of style
pairlist = [] # list of pairs to process
warnings = '' # list of Processed warnings
okformat = ['A','S','C'] # Anaglyph/Side-by-Side/Crossover
okstyle = ['N','P'] # Normal/Popout
okchar = ['0','1','2','3','4','5','6','7','8','9','L','R'] #last char in 2D filename
okext = ['jpg','JPG','jpeg','JPEG','png','PNG','tif','TIF','tiff','TIFF']
tifext = ['tif','TIF','tiff','TIFF']
processlist = False # whether to show only recently processed files 32X
runningfile = 'RUNNING' # to show that multiprocessing is running
stopfile = 'STOP' # to tell multiprocessing to stop
process = 'queue' # queue/process/reset
retain = False # whether to save aligned L and R images #43
balance = 1 # balance factor for left right balance in Anaglyphs #43
Mdatafold = '' # Meson read-only data files
Mlocale = '' # Meson language files
urlForum = 'https://popout3d.proboards.com/'
#-------------------------------------------------------------------------------
'''
Added by 'exalm':
$XDG_DATA_HOME defines the base directory relative to which user specific data files should be stored.
If $XDG_DATA_HOME is either not set or empty, a default equal to $HOME/.local/share should be used.
$XDG_CONFIG_HOME defines the base directory relative to which user specific configuration files should be stored.
If $XDG_CONFIG_HOME is either not set or empty, a default equal to $HOME/.config should be used.
exalm
XDG standard
OK HOMEFOLD for initial location for looking for photographs.
OK CONFIGFOLD for saving/loading the Config file.
WORKFOLD for temporary images, and STOP and RUNNING control files.
Try using XDG... folder, if that doesn't work use local folder, if that doesn't work, exit.
'''
#1.6.42 -----------------------------------------------------
# home folder
homefold = os.getenv('XDG_HOME') #try XDG_HOME
if homefold == None:
homefold = os.getenv('HOME') #if not try HOME
if homefold == None:
sys.exit('Cannot find homefold') #if not, exit
homefold = homefold + '/'
#-------------------------------------------------------------
# configuration folder for preference file
configfold = os.getenv('XDG_CONFIG_HOME') #try XDG_CONFIG_HOME
if configfold == None:
if not os.path.exists(homefold + '.config'): #if not, try .config
sys.exit('Cannot find .config') #if not, exit
else:
configfold = homefold + '.config/popout3d'
if not os.path.exists(configfold): #.config exists find .config/popout3d
result = os.system('mkdir ' + configfold) #try making .config/popout3d
if result !=0:
sys.exit('Cannot make .config/popout3d') #cannot make .config/popout3d so exit
configfold = configfold +'/'
#-------------------------------------------------------------
# work folder for STOP and image files being processed
workfold = os.getenv('XDG_DATA_HOME') #try XDG_DATA_HOME
if workfold == None:
if not os.path.exists(homefold + '.local/share'): #if not, try .local/share
sys.exit('Cannot find .local/share') #if not, exit
else:
if not os.path.exists(homefold + '.local/share/popout3d'): #find ...popout3d
result = os.system('mkdir ' + homefold + '.local/share/popout3d') #try making it
if result != 0:
sys.exit('Cannot make .local/share/popout3d') #if not, exit
workfold = homefold +'.local/share/popout3d/data' #ok so set to ....data
if os.path.isdir(workfold): #if it exists, remove workdir and its files
shutil.rmtree(workfold, True)
result = os.system('mkdir ' + workfold) #try making work directory
if result !=0:
sys.exit('Cannot make work directory') #cannot make work directory so exit
workfold = workfold +'/'
#Meson created directories------------------------------------
'''
Directories from Meson, if program isn't installed they won't exist.
To run it as a deb or RPM would require altering meson.build
Mdatafold source of read-only files like icons and grey image.
Mlocale source of language files
'''
# Mdtafold is project folder (read-only) for dummy image and blank image
# Mlocale is localisation folder for translation files
if os.path.exists('/.flatpak-info'): # Is it a Flatpak? This is within the sandbox so can't be seen
Mdatafold = '/app/share/popout3d/'
Mlocale = '/app/share/locale/' # gettext will add {language}/LC_MESSAGES to Mlocale
else: # no package for testing
Mdatafold = '/home/chris/git/popout3d/'
Mlocale = '/home/chris/git/locale/' # gettext will add {language}/LC_MESSAGES to Mlocale
#locale-----------------------------------------------------
locale.setlocale(locale.LC_ALL, '') # For words in GTK4 itself, locale to users default language (otherwise might be C/POSIX locale)
# To test GTK4 built-in words, use this in Bash: export LC_ALL=nl_NL.UTF-8
language = locale.getlocale()[0]
#language = 'nl_NL' # test
#language = 'de_DE' # test
lang = gettext.translation('popout3d', localedir=Mlocale, languages=[language], fallback=True)
_ = lang.gettext
# Help pages -------------------------------------------------------------------
helpPage = []
# Basics -----------------------------------------------------------------------
helpPage.append(_('''Take two photos of a stationary subject. Take one, then move the camera about 60mm to the right but pointing at the same thing and take another. Copy them to your PC, then rename them so that they have exactly the same name, but add an "L" at the end of the left-hand one, add an "R" to the end of the right-hand one. For example photoL.JPG and photoR.JPG.
Open Popout3D and select one of these photos with Open>File. Click Queue to see which 3D images will be created. The button changes to Process. Click Process to start processing. The button changes to Reset. If you use the < and > arrows, you will see a grey rectangle while the image is being created. When the 3D image is ready, it will be displayed if you use the arrows. This can take about half a minute.
You will need 3D glasses to see anaglyph images. You will need 3D Virtual Reality goggles to see side-by-side images. Some people can see side-by-side or crossover images without the goggles.'''))
# Source Files -----------------------------------------------------------------
helpPage.append(_('''Choose a stationary subject. Take one photo, then move the camera about 60mm to the right but pointing at the same thing, and take another. Always take them in sequence from left to right, so you don't get them mixed up. Popout3D may not be able to read/write image files on your camera or mobile phone and the 3D images you create will need extra space, so it's best to copy your original photos onto your PC. It is also easier to rename the photos on your PC. Rename them so that they have exactly the same name, but add an "L" at the end of the left-hand one, and add an "R" to the end of the right-hand one. For example photoL.JPG and photoR.JPG. If you find it difficult to get the spacing right, you can take 3 or more photos at different separations. Name them in order from left to right in numerical order, for example photo1.jpg, photo2.jpg and photo3.jpg. Don't take too many, as this results in creating a large number of 3D images, which will take a long time to process.
Each set of images (all those for the same subject) must be in the same format with the same file-extension - .jpg, .png or .tiff. They must be exactly the same size in pixels.
Movement
Stationary objects like buildings or scenery give good results. Pictures of people should work, provided they can keep still for a few seconds. Objects like trees and water may be work in the right circumstances, for example if there isn't too much wind, and the water is placid. Moving vehicles or people, or fast-flowing water like a waterfall or waves won't work.
Quality of Effect
A picture with objects at varying distances results in a convincing effect. Distant scenery won't work well, as there is little perspective effect anyway.
Notes
Some images are too difficult for the aligning software, and the resulting image is unusable.
For Anaglyph 3D avoid images with all red or all cyan objects. They will look odd as they only appear in one eye.
Because the 2D images must have exactly the same width and height in pixels, editing them is difficult. It can be done with a photo editor like rawTherapee, which shows you the size that the edited image will have, so you can make them match. It is easier to edit the 3D image, although you can't crop Side-By-Side or Crossover ones.
Nearby objects can cause strange effects when the camera's depth of field is high. A shorter exposure will reduce the depth of field.'''))
# File Selection----------------------------------------------------------------
helpPage.append(_('''File
To process a single set of images which are all for the same subject, first use Open>File to choose any file from the set.
scenery1.jpg
scenery2.jpg
scenery3.jpg
Selecting any of these files will prepare you to process all of them. The program will create a 3D image for each pair of originals, so you are able to choose the best combination of images for left and right. It is not recommended to use more than 3 originals as the number of output images goes up dramatically:
2 originals produce 1 3D image
3 originals produce 3 3D images
4 originals produce 6 3D images
5 originals produce 10 3D images
All the 3D images get the same filename (except the final character) and extension as the originals, plus two digits and a letter each for the format and style. These examples are "Anaglyph" format with "Normal" style:
scenery12AN.jpg was made with scenery1.jpg for the left image and scenery2.jpg for the right.
scenery13AN.jpg was made with scenery1.jpg for the left image and scenery3.jpg for the right.
With images ending in L and R you would get sceneryLRAN.jpg.
Folder:
To process all the image sets in a folder, first use Open>Folder to choose the folder with the sets of images.'''))
# Options for 3D Images --------------------------------------------------------
helpPage.append(_('''The format of the 3D image may be:
Anaglyph
A red/cyan colour 3D image viewed with coloured spectacles. These are available very cheaply on the Web.
Side-by-side
A side-by-side 3D image viewed straight ahead. left-hand image on the left, right-hand on the right. Some people can see these without a viewer, some can't.
Crossover
A side-by-side 3D image viewed with eyes crossed. right-hand image on the left, left-hand on the right. Some people can see these without a viewer, some can't.
There are two styles available:
Normal
A normal 3D image with the front of the picture level with the screen.
Popout
A "popout" image. In some cases the effect is startling, as the front of the 3D image will popout in front of the screen. In most cases there is little or no difference from "Normal".
Aligned 2D images
The processing creates aligned versions of left and right images, which are then merged into a 3D image. These two images are only temporary. If you want to try your own method of creating and displaying 3D images, you can save these two images by selecting "Aligned 2D Images > Save". They have names like PhotoL(LRSN).tif, the "PhotoL" refers to the original left-hand image. The "-LRSN" refers to the 3D image for which they were created. The "S" is irrelevant, but the "P" indicates that you a 3D image you make from it will be a popout one.'''))
# Processing -------------------------------------------------------------------
helpPage.append(_('''For the 3D effect to work it is essential that each pair of images is perfectly aligned vertically and rotationally. This is a vital step and it is very difficult to achieve when holding the camera and even when using image editing software. The program does this for you, it may take about 20 seconds per 3D image.
An existing 3D image file will not be overwritten. Therefore if you wish to recreate a 3D image, you will first need to move, rename or delete the existing 3D image.
To start processing the selected images, click on "Queue", this will show a list of the images to be created in the panel to the left and the button will change to "Process". If you don't like the list, choose another File/Folder or Format/Style.
If you are happy with this list, press "Process", the button will change to "Reset". You can use the < and > buttons to look for completed 3D images. Only the recently processed images are shown. When you have finished checking them, press "Reset" to go back to the File or Folder selection, now including the new images. Using the Open menu or the Delete or process buttons will also reset the list.
Notes
Portrait images from a mobile phone may be landscape photos with a rotation tag, the program rotates them to portrait for processing, but it doesn't change the originals.
Mobile mobile phones from one manufacturer are suspected of producing 16:9 photos which do not conform to JPG standards.
The 3D images won't have valid EXIF tags.
Settings from previous versions of the program are not loaded.
If you can't remember which image was left and which was right, create a 3D image as usual. If it doesn't look right, try the glasses on upside down, so the lenses swap sides. If the image now works rename the 2D images and create a new 3D image.
You can run Hugin yourself to experiment with other settings, it is available as a Flatpak.'''))
# View -------------------------------------------------------------------------
helpPage.append(_('''Scroll backwards and forwards through the images using < and >.
All
All the images in the chosen folder or set which follow the naming rules will be shown.
2D
Only 2D images in the chosen folder or set which follow the naming rules will be shown.
3D
Only 3D images in the chosen folder or set which follow the naming rules will be shown.
Triptych
3D images will be shown at the top with the 2D images they were made from shown beneath.
3D Image Types
These selections only affect 3D/Triptych views (see Processing for explanations). If you have created more than one type of 3D image, you won't need to change viewing devices as you browse the images, as these buttons allow you to restrict which image types are shown.
Delete
If a 3D image is being displayed you may delete it. To ensure that you don't lose original images, 2D images cannot be deleted from within Popout3D. You could of course delete them using your file manager.
Notes
You may need to view 3D images from further away than you might expect.'''))
#-------------------------------------------------------------------------------
# tooltips
tipDelete = _('Delete a 3D image')
tipQueue = _('Make a queue of 3D images from the files selected by Folder or File')
tipProcess = _('Create the 3D images shown in the queue')
tipReset = _('Clear the list of processed images and show the list selected by Folder or File')
tipNext = _('Show next image')
tipPrev = _('Show previous image')
tipOpen = _('Select a folder or file')
tipStripey = _('Menu')
tip2D = _('Show only 2D images')
tipTriptych = _('Show 3D images above the 2D images they came from')
tip3D = _('Show only 3D images')
tipAll = _('Show all images')
tipAnaglyph = _('A 3D image with the left-hand image red and the right-hand image cyan')
tipSidebyside = _('A 3D image with the left-hand image on the left and the right-hand image on the right')
tipCrossover = _('A 3D image with the left-hand image on the right and the right-hand image on the left')
tipNormal = _('A normal 3D image')
tipPopout = _('A 3D image which may appear to stand out in front of the screen')
tipRetain = _('Save the aligned 2D images which were created by align-image-stack') #43
#===============================================================================
def on_close_request(app): # When window is closed with X
with open(workfold + stopfile, 'w') as fn:
fn.write('STOP'+'\n')
fn.close()
def on_activate(app):
global viewind, process
##global image1, imageL, imageR, boxOuter1, boxImageL, boxImageR
win = Gtk.ApplicationWindow(application=app)
win.maximize()
win.header = Gtk.HeaderBar()
win.set_titlebar(win.header)
win.present()
win.connect('close-request', on_close_request)
#-----------------------------------------------------------------------------
def greyout(state): #43
if state:
GtickViewAnaglyph.set_sensitive(False)
GtickViewSidebyside.set_sensitive(False)
GtickViewCrossover.set_sensitive(False)
GtickViewNormal.set_sensitive(False)
GtickViewPopout.set_sensitive(False)
#GbuttonDelete.set_sensitive(False)
else:
GtickViewAnaglyph.set_sensitive(True)
GtickViewSidebyside.set_sensitive(True)
GtickViewCrossover.set_sensitive(True)
GtickViewNormal.set_sensitive(True)
GtickViewPopout.set_sensitive(True)
#GbuttonDelete.set_sensitive(True)
def ask(widget, response, param):
global viewind, process
if response == Gtk.ResponseType.OK:
if param == 'settings':
with open(configfold + settingsfile, 'w') as fn:
try:
fn.write(version+'\n')
fn.write(myfold+'\n')
fn.write(myfile+'\n')
fn.write(myext+'\n')
fn.write(formatcode+'\n')
fn.write(stylecode+'\n')
fn.write(viewDim+'\n')
fn.write(viewType+'\n')
if retain:
fn.write('True\n') #43
else:
fn.write('False\n') #43
fn.write(str(balance)+'\n') #43
except:
print('Failed to write preference file')
elif param == 'delete':
ok = True
try:
os.remove(myfold + viewlist[viewind][0]+'.'+viewlist[viewind][1])
except:
ok = False
showInfo(_('File not found'))
if ok:
if process == 'reset':
labelInfoTitle.set_markup('<b>' + _(scope) + ' ' + _('Selection') +'</b>')
GbuttonProcess.set_label(_('Queue')); process = 'queue'
makeviewlist(False); viewind = 0
findNext('<'); showImage()
widget.destroy()
#-----------------------------------------------------------------------------
def showInfo(messtext):
message = Gtk.MessageDialog(title = _('Information'), text = messtext)
message.add_buttons(_('OK'), Gtk.ResponseType.OK)
message.set_transient_for(win); message.set_modal(win)
message.set_default_response(Gtk.ResponseType.OK)
message.connect('response', ask, None)
message.set_visible(True)
def showDelete():
message = Gtk.MessageDialog(title = _('Are you sure?'),
text = _('This will delete') +' ' + viewlist[viewind][0]+'.'+viewlist[viewind][1])
message.add_buttons(_('Cancel'), Gtk.ResponseType.CANCEL, _('Delete'), Gtk.ResponseType.OK) #43
message.set_transient_for(win); message.set_modal(win)
message.set_default_response(Gtk.ResponseType.OK)
message.connect('response', ask, 'delete')
message.set_visible(True)
def showSettings(action, button):
message = Gtk.MessageDialog(title = _('Are you sure?'), text = _('This will save your current settings as the defaults'))
message.set_transient_for(win); message.set_modal(win)
message.add_buttons(_('Cancel'), Gtk.ResponseType.CANCEL, _('Save'), Gtk.ResponseType.OK) #43
message.set_default_response(Gtk.ResponseType.OK)
message.connect('response', ask, 'settings')
message.set_visible(True)
def showAbout(action, button):
message = Gtk.AboutDialog(transient_for=win, modal=True)
message.set_logo_icon_name('com.github.PopoutApps.popout3d')
message.set_program_name('Popout3D')
message.set_version(version)
message.set_comments(_('Create a 3D image from ordinary photographs'))
message.set_website_label('Popout3D ' + _('on') + ' GitHub')
message.set_website('https://github.com/PopoutApps/popout3d')
message.set_copyright(_('Copyright') + ' 2022, 2023 Chris Rogers')
message.set_license_type(Gtk.License.GPL_3_0)
message.set_authors(['PopoutApps'])
message.add_credit_section(section_name=_('Image Alignment'), people=['align-image-stack']) #43
message.add_credit_section(section_name='Flatpak', people=['Alexander Mikhaylenko', 'Hubert Figuière', 'Bartłomiej Piotrowski','Nick Richards'])
message.set_visible(True)
def showHelp(action, button):
message = Gtk.Window()
message.set_title(_('How to use Popout3D'))
message.set_default_size(1000, 700)
message.set_child(notebook)
message.set_transient_for(win); message.set_modal(win)
message.set_visible(True)
def showForum(action, button):
result = webbrowser.open(urlForum)
#def showLocale(action, button):
# result = webbrowser.open(urlCrowdin)
def readSettings():
global version, myfold, myfile, myext, formatcode, stylecode, viewDim, scope, viewType, firstrun, retain, balance #43
# local okpref, okcol, ver, i
# Preference file is not present when program is installed, so shows whether this is the first run.
# create settings array
prefdata = []
for i in range(10): #43
prefdata.append('')
# load settings file, if file not found or is wrong version skip to end
# if any fields are bad, replace with default
okpref = True
try:
with open(configfold + settingsfile, 'r') as infile:
ver = infile.readline()[:-1]
if ver != version:
okpref = False
except:
okpref = False
if okpref:
try:
with open(configfold + settingsfile, 'r') as infile:
for i in range(0, 10): #43
prefdata[i] = infile.readline()
prefdata[i] = prefdata[i][:-1]
except:
okpref = False
if okpref:
if os.path.exists(prefdata[1]):
myfold = prefdata[1]
else:
myfold = homefold #1.5.31
myfile = prefdata[2] ; myext = prefdata[3]; scope = 'File'
#153 note this doesn't check the ? character is valid, but it shouldn't be possible
# to save an invalid one. Should only occur if preference file was edited
if not glob.glob(myfold+'/'+myfile+'?.'+myext):
myfile == ''; myext == ''; scope = 'Folder' #1.5.31
if prefdata[4] in ['A', 'S', 'C']: # Anaglyph/Side-by-Side/Crossover
formatcode = prefdata[4]
else:
formatcode = 'A'
if prefdata[5] in ['N','P']: # Normal (Level)/Popout
stylecode = prefdata[5]
else:
stylecode = 'N'
if prefdata[6] in ['All', '2D', '3D', 'Triptych']:
viewDim = prefdata[6]
else:
viewDim = 'All'
if (prefdata[7][0] in 'A-' and prefdata[7][1] in 'S-' and prefdata[7][2] in 'C-'
and prefdata[7][3] in 'N-' and prefdata[7][4] in 'P-'):
viewType = prefdata[7]
else:
viewType = 'ASCNP'
if prefdata[8] in ['True', 'False']: #43
if prefdata[8] == 'True':
retain = True
else:
retain = False
else:
retain = False
try: #43
balance = float(prefdata[9]) #43
if not (balance >= .5 and balance <= 1.5):
balance = 1
except:
balance = 1
if okpref:
firstrun = False
else: # defaults
#version already set
myfold = homefold #1.5.31
myfile = '' #1.5.31
myext = '' #1.5.31
formatcode = 'A'
stylecode = 'N'
viewDim = 'All'
viewType = 'ASCNP'
retain = False #43
balance = 1 #43
# write pref file anyway in case mydir/myfile have been deleted or any other problem
with open(configfold + settingsfile, 'w') as fn:
fn.write(version+'\n')
fn.write(myfold+'\n')
fn.write(myfile+'\n')
fn.write(myext+'\n')
fn.write(formatcode+'\n')
fn.write(stylecode+'\n')
fn.write(viewDim+'\n')
fn.write(viewType+'\n')
if retain:
fn.write('True\n') #43
else:
fn.write('False\n') #43
fn.write(str(balance)+'\n') #43
print('<<',balance,'>>')
#-----------------------------------------------------------------------------
def exif(newfilename, newext):
'''
Open file, get tags, if processing turn and save the image.
Returns orientation, except 'notfound' if file missing and 'tagerror' if exif flags not available.
'''
global pixbuf, error
# local orientationTag
orientationTag = 'None'
if os.path.isfile(myfold+newfilename+'.'+newext):
try:
image = Image.open(myfold+newfilename+'.'+newext)
except:
return 'notfound'
else:
return 'notfound'
try:
image_exif = image.getexif()
except AssertionError as error:
print('image.exif() error: ', error)
image.close
return 'tagerror'
for tag_id in image_exif:
tag=TAGS.get(tag_id,tag_id)
data=image_exif.get(tag_id)
if isinstance(data,bytes):
try:
data=data.decode()
except:
image.close
return 'tagerror'
#print(f'{tag:20}:{data}')
if tag == 'Orientation':
orientationTag = str(data)
image.close
return orientationTag
#-----------------------------------------------------------------------------
def findVmatch():
global viewind # local searchlength, charL, charR, viewindL, viewindR, viewindN
if len(viewlist) > 0: #and viewDim != 'All': # Can use same image if switching viewDim to All
searchfilename = viewlist[viewind][0]; searchlength = -1
searchext = viewlist[viewind][1]; imageDim = viewlist[viewind][2]
charL = charR = ''; viewindL = viewindR = viewindN = -1
if not (viewDim == 'All' or viewDim == imageDim or (viewDim == 'Triptych' and imageDim == '3D')):
# switching viewDim to 2D
# get basic filename, length and 2 characters
if viewDim == '2D' and imageDim == '3D':
searchname = searchfilename[:-4]; searchlength = len(searchname)
charL = searchfilename[-4]; charR = searchfilename[-3]
# search through list
viewindL = viewindR = viewindN = -1
for record in viewlist:
if record[1] == searchext and record[2] == '2D':
if viewindL < 0 and searchname+charL in record[0][:searchlength+1]:
viewindL = viewlist.index(record) # L of 3D image matches a 2D image
elif viewindR < 0 and searchname+charR in record[0][:searchlength+1]:
viewindR = viewlist.index(record) # R of 3D image matches a 2D image
elif viewindN < 0 and searchname in record[0][:searchlength]:
viewindN = viewlist.index(record) # match for basic name
# switching viewDim to 3D
# get basic filename and 1 character
elif viewDim in ('3D', 'Triptych') and imageDim == '2D':
searchname = searchfilename[:-1]; searchlength = len(searchname)
charL = searchfilename[-1]; charR = ''
# search through list
viewindL = viewindR = viewindN = -1
for record in viewlist:
if (record[1] == searchext and record[2] == '3D'
and record[0][-2] in viewType and record[0][-1] in viewType):
if viewindL < 0 and searchname+charL in record[0][:searchlength+1]:
viewindL = viewlist.index(record) # char of 2D image matches 1st char of a 3D image
elif viewindR < 0 and searchname+charL in record[0][:searchlength]+record[0][-3]:
viewindR = viewlist.index(record) # char of 2D image matches 2nd char of a 3D image
elif viewindN < 0 and searchname in record[0][:searchlength]:
viewindN = viewlist.index(record) # match for basic name
if viewindL > -1:
viewind = viewindL
elif viewindR > -1:
viewind = viewindR
elif viewindN > -1:
viewind = viewindN
else:
viewind = 0
#-----------------------------------------------------------------------------
def findFSmatch():
global viewind # local searchlength, charL, charR, viewindL, viewindR, viewindN
if len(viewlist) > 0:
# Details of current image
searchfilename = viewlist[viewind][0]; searchext = viewlist[viewind][1]
searchname = searchfilename[:-4]; searchlength = len(searchname)
charL = searchfilename[-4]; charR = searchfilename[-3]
charF = searchfilename[-2]; charS = searchfilename[-1]
# might be still valid, if not search viewlist
if not (charF in viewType and charS in viewType):
viewindLR = viewindL = viewindR = viewindName = -1
for record in viewlist:
if (record[1] == searchext and record[2] == '3D'
and record[0][-2] in viewType and record[0][-1] in viewType
):
if viewindLR < 0 and searchname+charL+charR in record[0][:searchlength+2]:
viewindLR = viewlist.index(record) # both L and R chars match
# rest same as 2D to 3D
elif viewindL < 0 and searchname+charL in record[0][:searchlength+1]:
viewindL = viewlist.index(record) # charL matches
elif viewindR < 0 and searchname+charL in record[0][:searchlength]+record[0][-3]:
viewindR = viewlist.index(record) # charR matches
elif viewindName < 0 and searchname in record[0][:searchlength]:
viewindName = viewlist.index(record) # only basic name matches
if viewindLR > -1:
viewind = viewindLR
elif viewindL > -1:
viewind = viewindL
elif viewindR > -1:
viewind = viewindR
elif viewindName > -1:
viewind = viewindName
else:
viewind = 0
#-----------------------------------------------------------------------------
def findNext(direction):
global viewind
#local found
newviewind = viewind
if direction == '>':
newviewind = newviewind +1
else:
newviewind = newviewind -1
found = False
while found == False and len(viewlist) > 0 and newviewind > -1 and newviewind < len(viewlist):
if ((viewDim in ('All', '3D', 'Triptych') and viewlist[newviewind][2] == '3D'
and viewlist[newviewind][0][-2] in viewType and viewlist[newviewind][0][-1] in viewType)
or (viewDim in ('All', '2D') and viewlist[newviewind][2] == '2D')
):
found = True
viewind = newviewind
return
elif direction == '>':
newviewind = newviewind +1
else:
newviewind = newviewind -1
# if a further image wasn't found, leave viewind as it is.
#-----------------------------------------------------------------------------
def makepairlist(newfile, newformatcode, newstylecode, newext):
global warnings, pairlist, infolist, viewDim
# local imagestodo, imagesok
# find out how many images there are in this set
imagestodo = 0 ; imagesok = False
# check for one with a digit at end
for i in range (0, 10):
if os.path.isfile(myfold+newfile+str(i)+'.'+newext):
imagestodo = imagestodo + 1
if imagestodo > 1:
imagesok = True
# check for pair with L at end and R at end
if os.path.isfile(myfold+newfile+'L'+'.'+newext) and os.path.isfile(myfold+newfile+'R'+'.'+newext):
imagesok = True
if imagesok:
# loop through all valid image pairs
# digits at the end
leftn = 0 ; rightn = 1
while leftn < 9: # only single digits
# look for images ending in 1-9
rightn = leftn + 1
while rightn < 10: # top number is 9
# if left and right images exist and there is no existing 3D one
if (os.path.isfile(myfold+newfile+str(leftn)+'.'+newext) and os.path.isfile(myfold+newfile+str(rightn)+'.'+newext)
and not os.path.isfile(myfold+newfile+str(leftn)+str(rightn)+newformatcode+newstylecode+'.'+newext)) and [newfile, str(leftn), str(rightn), newformatcode, newstylecode, newext] not in pairlist:
tagL = exif(newfile+str(leftn), newext)
tagR = exif(newfile+str(rightn), newext)
pairlist.append([newfile, str(leftn), str(rightn), newformatcode, newstylecode, newext, tagL, tagR])
rightn = rightn + 1
leftn = leftn + 1
# look for images ending in L and R
if (os.path.isfile(myfold+newfile+'L'+'.'+newext) and os.path.isfile(myfold+newfile+'R'+'.'+newext)
and not os.path.isfile(myfold+newfile+'LR'+newformatcode+newstylecode+'.'+newext)) and [newfile, 'L', 'R', newformatcode, newstylecode, newext] not in pairlist:
tagL = exif(newfile+str(leftn), newext)
tagR = exif(newfile+str(rightn), newext)
pairlist.append([newfile, 'L', 'R', newformatcode, newstylecode, newext, tagL, tagR])
#-------------------------------------------------------------------------------
def makeviewlist(queuing):
global viewlist, viewind, pairlist
#local variables newfile, newext, infolist, tag
tag = 'TAG'
viewlist = []
if queuing:
# add all relevant images to viewlist
for record in pairlist:
#viewlist.append([record[0]+record[1], record[5], '2D', record[6]])
viewlist.append([record[0]+record[1]+record[2]+record[3]+record[4], record[5], '3D', ''])
#viewlist.append([record[0]+record[2], record[5], '2D', record[7]])
else:
# 2D
for newfile in os.listdir(myfold):
newfile, newext = os.path.splitext(newfile) ; newext = newext[1:]
if len(newfile) > 1:
if newext in okext and newfile[-1] in okchar:
# set: only add files in set to viewlist
# file
if scope == 'File':
if len(newfile) == len(myfile) +1:
if newfile[0:-1] == myfile and newext == myext:
tag = exif(newfile, newext)
viewlist.append([newfile, newext, '2D', tag])
# folder
elif scope == 'Folder':
tag = exif(newfile, newext)
viewlist.append([newfile, newext, '2D', tag])
# 3D
for newfile in os.listdir(myfold):
newfile, newext = os.path.splitext(newfile) ; newext = newext[1:]
if len(newfile) > 4:
if (newext in okext
and newfile[-4] in okchar and newfile[-3] in okchar
and newfile[-2] in okformat and newfile[-1] in okstyle
and newfile[-2] in viewType and newfile[-1] in viewType):
# file
if scope == 'File':
if len(newfile) == len(myfile) +4:
if newfile[0:-4] == myfile and newext == myext:
tag = exif(newfile, newext)
viewlist.append([newfile, newext, '3D', tag])
# folder
elif scope == 'Folder':
tag = exif(newfile, newext)
viewlist.append([newfile, newext, '3D', tag])
viewlist = sorted(viewlist)
# make infolist
infolist = ''
for i in viewlist:
if (i[2] == '2D' and not process == 'reset') or i[2] == '3D':
infolist = infolist+i[0]+'.'+i[1]+'\n'
labelInfo.set_text(infolist)
#-----------------------------------------------------------------------------
def showImage():
global image1, imageL, imageR, boxOuter1, boxOuterL, boxOuterR
#local newfilename1, newfilenameL, newfilenameR, newext1, newextL, newextR
labelImage1.set_text('')
labelImageL.set_text('')
labelImageR.set_text('')
for i in boxImage1:
boxImage1.remove(i)
for i in boxImageL:
boxImageL.remove(i)
for i in boxImageR:
boxImageR.remove(i)
if len(viewlist) > 0 and viewind > -1 and (
(viewDim in ('All', '3D', 'Triptych') and viewlist[viewind][2] == '3D'
and viewlist[viewind][0][-2] in viewType and viewlist[viewind][0][-1] in viewType
)
or
(viewDim in ('All', '2D') and viewlist[viewind][2] == '2D')
):
if viewlist[viewind][2] == '2D': #43 greyout delete button for 2D images
GbuttonDelete.set_sensitive(False)
else:
GbuttonDelete.set_sensitive(True)
# two 2D images for triptych
if viewDim == 'Triptych':
# Left 2D image
newfilenameL = viewlist[viewind][0][:-4]+viewlist[viewind][0][-4]; newextL = viewlist[viewind][1]
labelImageL.set_markup('<b>'+newfilenameL+'.'+newextL+'</b>')
if os.path.isfile(myfold+newfilenameL+'.'+newextL):
orientationL = viewlist[viewind][3]
if orientationL == '3': # upside down, so rotate 180 clockwise
pixbuf = GdkPixbuf.Pixbuf.rotate_simple(GdkPixbuf.Pixbuf.new_from_file(myfold+newfilenameL+'.'+newextL), 180)
elif orientationL == '6': # top at right so rotate 270 clockwise
pixbuf = GdkPixbuf.Pixbuf.rotate_simple(GdkPixbuf.Pixbuf.new_from_file(myfold+newfilenameL+'.'+newextL), 270)
elif orientationL == '8': #top pointing left so rotate 90 clockwise
pixbuf = GdkPixbuf.Pixbuf.rotate_simple(GdkPixbuf.Pixbuf.new_from_file(myfold+newfilenameL+'.'+newextL), 90)
elif orientationL == 'notfound':
pixbuf = GdkPixbuf.Pixbuf.new_from_file(Mdatafold+dummyfile)
else:
pixbuf = GdkPixbuf.Pixbuf.new_from_file(myfold+newfilenameL+'.'+newextL)
else:
pixbuf = GdkPixbuf.Pixbuf.new_from_file(Mdatafold+dummyfile)
imageL = Gtk.Picture.new_for_pixbuf(pixbuf)
imageL.props.hexpand = True
imageL.props.content_fit = Gtk.ContentFit.CONTAIN
boxImageL.append(imageL)
# Right 2D image
newfilenameR = viewlist[viewind][0][:-4]+viewlist[viewind][0][-3]; newextR = viewlist[viewind][1]
labelImageR.set_markup('<b>'+newfilenameR+'.'+newextR+'</b>')
if os.path.isfile(myfold+newfilenameR+'.'+newextR):
orientationR = viewlist[viewind][3]
if orientationR == '3': # upside down, so rotate 180 clockwise
pixbuf = GdkPixbuf.Pixbuf.rotate_simple(GdkPixbuf.Pixbuf.new_from__file_at_scale(myfold+newfilenameR+'.'+newextR), 180)
elif orientationR == '6': # top at right so rotate 270 clockwise
pixbuf = GdkPixbuf.Pixbuf.rotate_simple(GdkPixbuf.Pixbuf.new_from_file(myfold+newfilenameR+'.'+newextR), 270)
elif orientationR == '8': #top pointing left so rotate 90 clockwise
pixbuf = GdkPixbuf.Pixbuf.rotate_simple(GdkPixbuf.Pixbuf.new_from_file(myfold+newfilenameR+'.'+newextR), 90)
elif orientationR == 'notfound':
pixbuf = GdkPixbuf.Pixbuf.new_from_file(Mdatafold+dummyfile)
else:
pixbuf = GdkPixbuf.Pixbuf.new_from_file(myfold+newfilenameR+'.'+newextR)
else:
pixbuf = GdkPixbuf.Pixbuf.new_from_file(Mdatafold+dummyfile)
imageR = Gtk.Picture.new_for_pixbuf(pixbuf)
imageR.props.hexpand = True
imageR.props.content_fit = Gtk.ContentFit.CONTAIN
boxImageR.append(imageR)
# image 1 must be last so it doesn't take all the space in a Triptych
newfilename1 = viewlist[viewind][0]; newext1 = viewlist[viewind][1]
labelImage1.set_markup('<b>'+newfilename1+'.'+newext1+'</b>')
if os.path.isfile(myfold+newfilename1+'.'+newext1):
orientation1 = viewlist[viewind][3]
if orientation1 == '3': # upside down, so rotate 180 clockwise
pixbuf = GdkPixbuf.Pixbuf.rotate_simple(GdkPixbuf.Pixbuf.new_from_file(myfold+newfilename1+'.'+newext1), 180)
elif orientation1 == '6': # top at right so rotate 270 clockwise
pixbuf = GdkPixbuf.Pixbuf.rotate_simple(GdkPixbuf.Pixbuf.new_from_file(myfold+newfilename1+'.'+newext1), 270)
elif orientation1 == '8': #top pointing left so rotate 90 clockwise
pixbuf = GdkPixbuf.Pixbuf.rotate_simple(GdkPixbuf.Pixbuf.new_from_file(myfold+newfilename1+'.'+newext1), 90)
elif orientation1 == 'notfound': # file missing #redundant
pixbuf = GdkPixbuf.Pixbuf.new_from_file(Mdatafold+dummyfile)
else: #already upright or could not be determined
pixbuf = GdkPixbuf.Pixbuf.new_from_file(myfold+newfilename1+'.'+newext1)
else: # not found
pixbuf = GdkPixbuf.Pixbuf.new_from_file(Mdatafold+dummyfile)
else:
pixbuf = GdkPixbuf.Pixbuf.new_from_file(Mdatafold+dummyfile)
image1 = Gtk.Picture.new_for_pixbuf(pixbuf)
image1.props.hexpand = True; image1.props.vexpand = True
image1.props.content_fit = Gtk.ContentFit.CONTAIN
boxImage1.append(image1)
#-----------------------------------------------------------------------------
def checkpairlist():
global pairlist
#local warnings, existsLeft, existsRight, imageLeftFormat, imageRightFormat, imageLeftSize, imageRightSize
warnings = ''
for i in pairlist: # open images and get image type and size
try:
image = Image.open(myfold+i[0]+i[1]+'.'+i[5])
imageLeftFormat = image.format ; imageLeftSize = image.size
existsLeft = True
image.close
except:
warnings = warnings + _('Unable to load left image') +' ' +myfold+i[0]+i[1]+'.'+i[5]+'.\n'
existsLeft = False
pairlist.remove(i)
try:
image = Image.open(myfold+i[0]+i[2]+'.'+i[5])
imageRightFormat = image.format ; imageRightSize = image.size
existsRight = True
image.close
except:
warnings = warnings + _('Unable to load right image') +' ' +myfold+i[0]+i[2]+'.'+i[5]+'.\n'
existsRight = False
pairlist.remove(i)
if existsLeft and existsRight:
if imageLeftFormat != imageRightFormat:
warnings = warnings +i[0]+i[1]+'.'+i[5]+ ' ' +_('and') +' ' +i[0]+i[2]+'.'+i[5] +' ' + _('cannot be used as they have different filetypes.') +'\n'
pairlist.remove(i)
if imageLeftSize != imageRightSize:
warnings = warnings +i[0]+i[1]+'.'+i[5]+' '+str(imageLeftSize) +' ' +_('and') +' ' +i[0]+i[2]+'.'+i[5]+' '+str(imageRightSize)+ ' ' +_('cannot be used as their dimensions do not match.') +'\n'
pairlist.remove(i)
if warnings != '':
showInfo(warnings)
#-----------------------------------------------------------------------------
def processPairlist(pairlist):
#global warnings
#local tagL, tagR, foldL, foldR
with open(workfold + runningfile, 'w') as fn:
fn.write('RUNNING'+'\n')
fn.close()
for record in pairlist:
if os.path.isfile(workfold+stopfile):
break
else:
newfile = record[0]; leftn = record[1]; rightn = record[2]; newformatcode = record[3]; newstylecode = record[4]; newext = record[5]; tagL = record[6]; tagR = record[7]