-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuginPanorama.lua
executable file
·946 lines (789 loc) · 34.2 KB
/
HuginPanorama.lua
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
--[[
HuginPanorama.lua - process images via Hugin
Copyright (C) 2024 Fiona Boston <fiona@fbphotography.uk>.
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/>.
================================================================
Create panorama using Hugin tools
When Hugin completes, the result is imported and optionally grouped with the original files.
ADDITIONAL SOFTWARE NEEDED FOR THIS SCRIPT
Hugin and associated commandline tools - https://sourceforge.net/projects/hugin/
ExifTool by Phil Harvey - https://exiftool.org
OPTIONAL ADDITIONAL SOFTWARE USED BY SCRIPT IF INSTALLED
LuaFileSystem - https://luarocks.org/modules/hisham/luafilesystem
See associated HuginPanorama-Readme.md for installation / running options
]]
-- *************************************************
-- Setup and Initialisation
-- *************************************************
local dt = require 'darktable'
local du = require "lib/dtutils"
local df = require 'lib/dtutils.file'
local dsys = require 'lib/dtutils.system'
-- lua libraries installed via luarocks https://github.com/luarocks/luarocks/wiki
-- luafilesystem - https://lunarmodules.github.io/luafilesystem/index.html - file system functions
-- additional validation and output checking is activated if lfs is present
local lfs_loaded,lfs = pcall(require,'lfs')
if lfs_loaded == false then
dt.print_log("No lfs module")
else
dt.print_log("lfs module found")
end
-- luaexpat - https://lunarmodules.github.io/luaexpat/index.html - XML Expat parsing
-- not currently used but future enhancement around chcking ZereneBatch.xml may require it
local xml_loaded,lxp = pcall(require,'lxp')
if xml_loaded == true then
dt.print_log("lxp module found")
else
dt.print_log("No lxp module")
end
du.check_min_api_version("7.0.0", "HuginProcessor")
-- return data structure for script_manager
local script_data = {}
script_data.metadata = {
name = "HuginPanorama",
purpose = "Create panorama using Hugin and tools",
author = "Fiona Boston <fiona@fbphotography.uk>",
help = "https://github.com/fjb2020/darktable-scripts"
}
local temp
script_data.destroy = nil -- function to destory the script
script_data.destroy_method = nil -- set to hide for libs since we can't destroy them commpletely yet, otherwise leave as nil
script_data.restart = nil -- how to restart the (lib) script after it's been hidden - i.e. make it visible again
local GUI = { --GUI Elements Table
optionwidgets = {
label_run_options = {},
cbo_run_options = {},
label_import_options = {},
group = {},
copy_metadata = {},
copy_tags = {},
copy_exif = {},
add_tags_box = {},
add_tags_label = {},
add_tags = {},
},
options = {},
run = {},
}
local settings = {
hugin_bin = '',
hugin_tools = '',
stagingfolder = '',
exiftool_bin = '',
}
local paths = {
firstimagepath = '', -- this will be path that merged images get moved to prior to import
firstimagebase = '', -- basename of first image for naming output file
lastimagebase = '',
firstimagename = '',
lastimagename = '',
panoname = '', -- name of final output pano file
fullpanoname = '',
blendedname = '',
fusedname = '',
pto_path = '',
}
dt.print_log('Running on ' .. dt.configuration.running_os)
local mod = 'module_HuginProcessor'
local os_path_seperator = '/'
local os_quote = "'"
if dt.configuration.running_os == 'windows' then
os_path_seperator = '\\'
os_quote = '"'
end
-- find locale directory:
local scriptfile = debug.getinfo( 1, "S" )
local localedir = dt.configuration.config_dir .. os_path_seperator .. 'lua' .. os_path_seperator .. 'locale' .. os_path_seperator
--if scriptfile ~= nil and scriptfile.source ~= nil then
-- local path = scriptfile.source:match( "[^@].*[/\\]" )
-- localedir = path..os_path_seperator..'locale'
--end
dt.print_log( "localedir: "..localedir )
-- Tell gettext where to find the .mo file translating messages for a particular domain
local gettext = dt.gettext
gettext.bindtextdomain( 'HuginProcessor', localedir )
-- declare a local namespace and a couple of variables we'll need to install the module
local mE = {}
mE.event_registered = false -- keep track of whether we've added an event callback or not
mE.module_installed = false -- keep track of whether the module is module_installed
-- *************************************************
-- utility functions
-- *************************************************
local function _(msgid)
return gettext.dgettext( 'HuginProcessor', msgid )
end
-- *************************
local function export_image( image, exportfilename)
-- export the given single image to tiff (16 bit)
local curr_image = image.path..os_path_seperator..image.filename
dt.print_log( "exporting "..curr_image )
local exporter = dt.new_format("tiff")
exporter.bpp = 16
exporter:write_image(image, exportfilename)
dt.print_log( "exported file: "..exportfilename )
end
-- *************************
local function copy_tags( all_tags, image )
-- add tags on image to all_tags table
local image_tags = dt.tags.get_tags( image )
for _,tag in pairs( image_tags ) do
if string.match( tag.name, 'darktable|' ) == nil then
dt.print_log( "image: "..image.filename .. " tag: "..tag.name )
all_tags[ #all_tags + 1 ] = tag
end
end
dt.print_log( "#all_tags: ".. #all_tags )
end
-- *************************
local function insert_tags( image, tags )
for _,tag in pairs( tags ) do
dt.tags.attach(tag, image )
dt.print_log( 'image: '..image.filename..' adding tag ', tag.name )
end
end
-- *************************
--removes spaces from the front and back of passed in text
local function clean_spaces(text)
text = string.gsub(text,'^%s*','')
text = string.gsub(text,'%s*$','')
return text
end
-- *************************
local function add_additional_tags( image )
local set_tag = GUI.optionwidgets.add_tags.text
if set_tag ~= nil then -- add additional user-specified tags
for tag in string.gmatch(set_tag, '[^,]+') do
tag = clean_spaces(tag)
tag = dt.tags.create(tag)
dt.tags.attach(tag, image)
end
end
end
-- *************************
local function save_preferences()
dt.preferences.write( mod, 'group', 'bool', GUI.optionwidgets.group.value )
dt.preferences.write( mod, 'copy_metadata', 'bool', GUI.optionwidgets.copy_metadata.value )
dt.preferences.write( mod, 'copy_tags', 'bool', GUI.optionwidgets.copy_tags.value )
dt.preferences.write( mod, 'add_tags', 'string', GUI.optionwidgets.add_tags.text )
--dt.preferences.write( mod, 'use_gui', 'bool', GUI.optionwidgets.use_gui.value)
dt.preferences.write( mod, 'copy_exif', 'bool', GUI.optionwidgets.copy_exif.value)
end
-- *************************
local function load_preferences()
GUI.optionwidgets.group.value = dt.preferences.read( mod, 'group', 'bool' )
GUI.optionwidgets.copy_metadata.value = dt.preferences.read( mod, 'copy_metadata', 'bool' )
GUI.optionwidgets.copy_tags.value = dt.preferences.read( mod, 'copy_tags', 'bool' )
GUI.optionwidgets.add_tags.text = dt.preferences.read( mod, 'add_tags', 'string')
--GUI.optionwidgets.use_gui.value = dt.preferences.read(mod, 'use_gui', 'bool' )
GUI.optionwidgets.copy_exif.value = dt.preferences.read(mod, 'copy_exif', 'bool' )
end
-- *************************
-- stop running job
local function stop_job( job )
job.valid = false
end
-- *************************
local function sanitize_filename(filepath)
local path = df.get_path(filepath)
path = string.gsub( path, " ", "\\ " )
local basename = df.get_basename(filepath)
local filetype = df.get_filetype(filepath)
local sanitized = string.gsub(basename, " ", "\\ ")
return path .. sanitized .. "." .. filetype
end
-- **************************
local function CheckMacApp(Check_cmdline)
local cmdline = Check_cmdline
if dt.configuration.running_os == "macos" then
if string.sub(Check_cmdline,-5) == ".app'" or string.sub(Check_cmdline,-4) == ".app" then
-- user has entered .app folder rather than actual binary
cmdline = "open -W -a " .. Check_cmdline
end
end
return(cmdline)
end
-- **************************
local function run_app(app_name, app_cmdline)
local job = dt.gui.create_job( _"Running " .. app_name, true, stop_job )
dt.print_log( 'commandline: '..app_cmdline )
local app_start_time = os.date("*t",os.time())
dt.print_log(app_name .. " started at " .. app_start_time.hour ..":" .. app_start_time.min .. ":" .. app_start_time.sec)
local rv
if dt.configuration.running_os == 'windows' then
rv = dsys.windows_command( app_cmdline )
else
rv = dsys.external_command( app_cmdline )
end
dt.print_log( app_name .. ' returned '..tostring( rv ) )
if rv ~= 0 then
dt.print( _'could not start ' .. app_name .. ' error ' .. tostring(rv) )
end
local app_end_time = os.date("*t",os.time())
dt.print_log(app_name .. " finished at " .. app_end_time.hour ..":" .. app_end_time.min .. ":" .. app_end_time.sec)
if(job.valid) then
job.valid = false
end
return rv
end
-- **************************
local function getexif(image,exifdata)
-- reset exifdata tabke
for k in pairs(exifdata) do
exifdata[k] = nil
end
exifdata["Make"] = image.exif_maker
exifdata["Model"] = image.exif_model
exifdata["Lens"] = image.exif_lens
exifdata["Aperture"] = image.exif_aperture
exifdata["Exposure"] = image.exif_exposure
exifdata["Focal_Length"] = image.exif_focal_length
exifdata["ISO"] = image.exif_iso
exifdata["Create_Date"] = image.exif_datetime_taken
exifdata["Latitude"] = image.latitude
exifdata["Longitude"] = image.longitude
exifdata["Altitude"] = image.elevation
end
-- **************************
local function check_binary(binaryfile)
--- df.test_file(fname,'x') doesn't appear to work on windows
if dt.configuration.running_os == 'windows' then
if df.check_if_file_exists(binaryfile) then
return true
end
else
if df.test_file(binaryfile,'x') then
return true
end
end
return false
end
-- *************************************************
-- get settings set in Lua Options
-- *************************************************
local function get_settings()
local rv = 1
settings.hugin_bin = df.sanitize_filename( dt.preferences.read( mod, "hugin_Bin", "string" ) )
if check_binary(settings.hugin_bin) then
dt.print_log('Found Hugin binary ' ..settings.hugin_bin )
else
dt.print(_('Cannot find Hugin executable '.. settings.hugin_bin .. ' - please check parameters in global options -> Lua Options'))
dt.print_log('Cannot find Hugin executable '.. settings.hugin_bin)
return rv
end
settings.hugin_tools = df.sanitize_filename( dt.preferences.read( mod, "hugin_tools_directory", "string" ) )
if df.check_if_file_exists(settings.hugin_tools) then
dt.print_log('Found Hugin tools directory ' .. settings.hugin_tools )
else
dt.print(_('Cannot find hugin tools directory '.. settings.hugin_tools .. ' - please check parameters in global options -> Lua Options'))
dt.print_log('Cannot find hugin tools directory '.. settings.hugin_tools)
return rv
end
settings.stagingfolder = df.sanitize_filename( dt.preferences.read( mod, "HuginStagingFolder", "string" ) )
-- remove single quotes from folder name
settings.stagingfolder = string.gsub(settings.stagingfolder,"'","")
if not(df.check_if_file_exists(settings.stagingfolder)) then
dt.print(_('Cannot find staging folder '.. settings.stagingfolder .. ' - please check parameters in global options -> Lua Options'))
dt.print_log('Found staging folde ' .. settings.stagingfolder)
return rv
end
settings.exiftool_bin = df.sanitize_filename( dt.preferences.read( mod, "exiftool_bin", "string" ) )
if check_binary(settings.exiftool_bin) then
dt.print_log('Found exiftool binary ' .. settings.exiftool_bin )
else
dt.print(_('Cannot find exiftool executable '.. settings.exiftool_bin .. ' - please check parameters in global options -> Lua Options'))
dt.print_log('Cannot find exiftool executable '.. settings.exiftool_bin)
return rv
end
rv = 0
return rv
end
--- ************************************************
--- Run hugin via gui
--- ************************************************
local function hugin_gui(exp_image_list)
local hugin_cmd = CheckMacApp(settings.hugin_bin) .. ' ' .. exp_image_list
local rv = run_app('hugin...',hugin_cmd)
if rv ~= 0 then
dt.print_log("Unable to launch hugin gui - error " .. tostring(rv))
else
paths.pto_path = settings.stagingfolder .. os_path_seperator .. paths.lastimagename .. ' - ' .. paths.firstimagename .. '.pto'
paths.panoname = paths.lastimagename .. ' - ' .. paths.firstimagename .. '.tif'
paths.blendedname = settings.stagingfolder .. os_path_seperator .. paths.lastimagename .. ' - ' .. paths.firstimagename .. '_blended_fused.tif'
paths.fusedname = settings.stagingfolder .. os_path_seperator .. paths.lastimagename .. ' - ' .. paths.firstimagename .. '_fused.tif'
paths.fullpanoname = settings.stagingfolder .. os_path_seperator .. paths.lastimagename .. ' - ' .. paths.firstimagename .. '.tif'
end
return rv
end
--- ************************************************
--- Run hugin headless
--- ************************************************
local function hugin_headless(exp_image_list,exported_images_table,img_count)
--[[
details from https://wiki.panotools.org/Panorama_scripting_in_a_nutshell
pto_gen -o project.pto inputfile1, inputfile2, ...
cpfind -o project.pto --multirow --celeste project.pto
cpclean -o project.pto project.pto
linefind -o project.pto project.pto
autooptimiser -a -m -l -s -o project.pto project.pto
pano_modify --canvas=AUTO --crop=AUTO -o project.pto project.pto
nona -m TIFF_m -o project project.pto
enblend -o project.tif project0000.tif project0001.tif project0002.tif project003.tif
]]
local rv
local cmdline
paths.panoname = paths.lastimagebase .. '-' .. img_count .. '-huginpano.tif'
paths.fullpanoname = settings.stagingfolder .. os_path_seperator .. paths.lastimagebase .. '-' .. img_count .. '-huginpano.tif'
local project_files = {}
local project_file_list = ''
for i,exp_file_name in pairs(exported_images_table) do
project_files[i] = settings.stagingfolder .. os_path_seperator .. 'project' .. string.format("%04d",i-1) .. '.tif'
end
-- 1) Generate pto file
paths.pto_path = settings.stagingfolder .. os_path_seperator .. 'project.pto'
cmdline = settings.hugin_tools .. os_path_seperator .. 'pto_gen' .. ' ' .. exp_image_list .. '-o ' .. os_quote .. paths.pto_path .. os_quote
rv = run_app('pto_gen', cmdline)
-- 2) Find control points with cpfind, with celeste to ignore clouds
cmdline = settings.hugin_tools .. os_path_seperator .. 'cpfind -o ' .. os_quote .. paths.pto_path .. os_quote .. ' --multirow --celeste ' .. os_quote .. paths.pto_path .. os_quote
rv = run_app('cpfind', cmdline)
-- 3) Control point cleaning
cmdline = settings.hugin_tools .. os_path_seperator .. 'cpclean -o ' .. os_quote .. paths.pto_path .. os_quote .. ' ' .. os_quote .. paths.pto_path .. os_quote
rv = run_app('cpclean', cmdline)
-- 4) Find vertical lines
cmdline = settings.hugin_tools .. os_path_seperator .. 'linefind -o ' .. os_quote .. paths.pto_path .. os_quote .. ' ' .. os_quote .. paths.pto_path .. os_quote
rv = run_app('linefind', cmdline)
-- 5) Optimize position, do photometric optimization, straighten panorama and select suitable output projection
cmdline = settings.hugin_tools .. os_path_seperator .. 'autooptimiser -a -m -l -s -o ' .. os_quote .. paths.pto_path .. os_quote .. ' ' .. os_quote .. paths.pto_path .. os_quote
rv = run_app('autooptimiser', cmdline)
-- 6) Calculate optimal crop and optimal size
cmdline = settings.hugin_tools .. os_path_seperator .. 'pano_modify --canvas=AUTO --crop=AUTO -o ' .. os_quote .. paths.pto_path .. os_quote .. ' ' .. os_quote .. paths.pto_path .. os_quote
rv = run_app('pano_modify', cmdline)
-- 7) Remapping (Rendering) images
cmdline = settings.hugin_tools .. os_path_seperator .. 'nona -m TIFF_m -o ' .. os_quote .. settings.stagingfolder .. os_path_seperator .. 'project' .. os_quote .. ' ' .. os_quote .. paths.pto_path .. os_quote
rv = run_app('nona', cmdline)
-- 8) Blending
cmdline = settings.hugin_tools .. os_path_seperator .. 'enblend -o ' .. os_quote .. paths.fullpanoname .. os_quote .. ' '
for i,filename in pairs(project_files) do
cmdline = cmdline .. os_quote .. filename .. os_quote .. ' '
end
rv = run_app('enblend', cmdline)
-- clean up project files
for i,filename in pairs(project_files) do
os.remove(filename)
end
end
--- ************************************************
--- Create hdr (tonemapped)
--- ************************************************
local function create_hdr(exp_image_list,exported_images_table,img_count)
local rv
local project_files = {}
local project_file_list = ''
for i,exp_file_name in pairs(exported_images_table) do
project_files[i] = settings.stagingfolder .. os_path_seperator .. 'aligned' .. string.format("%04d",i-1) .. '.tif'
end
paths.panoname = paths.lastimagebase .. '-' .. img_count .. '-enfusehdr'
paths.fullpanoname = settings.stagingfolder .. os_path_seperator .. paths.panoname .. '.tif'
-- align images
local cmdline = settings.hugin_tools .. os_path_seperator .. 'align_image_stack' .. ' -a ' .. os_quote .. settings.stagingfolder .. os_path_seperator .. 'aligned' .. os_quote .. ' ' .. exp_image_list
rv = run_app('align_image_stack', cmdline)
paths.panoname = paths.panoname .. '.tif'
-- enfuse
cmdline = settings.hugin_tools .. os_path_seperator .. 'enfuse -o ' .. os_quote .. paths.fullpanoname .. os_quote .. ' '
for i,filename in pairs(project_files) do
cmdline = cmdline .. os_quote .. filename .. os_quote .. ' '
end
rv = run_app('enfuse', cmdline)
-- clean up project files
for i,filename in pairs(project_files) do
os.remove(filename)
end
end
-- **************************************************************
-- main function to run Hugin called by Process with Hugin button
-- **************************************************************
local function run_hugin_Process()
local exifdata = {}
local meta_data = { -- storage for metadata to apply to merged images
title = '',
description = '',
creator = '',
rights = '',
}
local source_img_table = {} -- table of source images
local exported_images_table = {} -- table of exported images
local all_tags = {} -- table of all tags from source images
local images_to_group = {} -- table of images to be grouped with first images
local exp_image_list = "" -- string of exported image filenames to pass to hugin binaries
local exiftool = true
local rv
dt.print_log( "starting hugin..." )
save_preferences()
-- Check existence of hugin binary files and staging folder
rv = get_settings()
if rv ~= 0 then
dt.print( _'could not locate required folders and binaries' )
end
-- check staging folder is empty - depends on lfsfilesystem which may not be available
if lfs_loaded then
local staging_clear = true
for this_file in lfs.dir(settings.stagingfolder) do
if this_file ~= "." and this_file ~= ".." then
dt.print_log(' Found '.. this_file)
if string.sub(this_file,1,1) ~= '.' then -- hidden files are ok
staging_clear=false
break
end
end
end
if not (staging_clear) then
dt.print_log(settings.stagingfolder .. " not empty")
dt.print(_("Please ensure the folder " .. settings.stagingfolder .. " is empty"))
return
end
end
-- **************************************************
-- pre processing - export images into staging folder
-- **************************************************
-- create a new progress_bar displayed in darktable.gui.libs.backgroundjobs
local job = dt.gui.create_job( _"exporting images to Hugin...", true, stop_job )
local images = dt.gui.selection() --get selected images
local img_count = #images
if img_count < 2 then --ensure enough images selected
dt.print(_('not enough images selected, select at least 2 images to process'))
if(job.valid) then
job.valid = false
end
return
end
for i,image in pairs(images) do
local export_file_name = settings.stagingfolder..os_path_seperator..image.filename..".tif"
export_image( image, export_file_name)
-- build list of exported image names to pass into hugin binaries
exp_image_list = exp_image_list .. os_quote .. export_file_name .. os_quote .. ' '
-- store tags for all exported images
copy_tags( all_tags, image )
-- store exported image name
table.insert(exported_images_table,export_file_name)
-- store source image for later grouping, tagging etc
source_img_table[ #source_img_table+1] = image
if i == 1 then
-- get path of first source image - merged files will be moved to this folder prior to import to DT
dt.print_log( 'image.path= '..image.path )
paths.firstimagepath = df.sanitize_filename( image.path )
paths.firstimagepath = string.gsub(paths.firstimagepath,"'","")
paths.firstimagepath = string.gsub(paths.firstimagepath,'"','')
paths.firstimagebase= df.get_basename(image.filename)
paths.firstimagename = df.get_filename(image.filename)
meta_data.title = image.title
meta_data.description = image.description
meta_data.creator = image.creator
meta_data.rights = image.rights
-- get exif data if requested
if GUI.optionwidgets.copy_exif.value == true then
getexif(image,exifdata)
end
else
-- remember image to group later:
images_to_group[ #images_to_group + 1 ] = image
end
if i == img_count then
-- last image
paths.lastimagename = df.get_filename(image.filename)
paths.lastimagebase = df.get_basename(image.filename)
end
if dt.control.ending or not job.valid then
dt.print_log( _"exporting images canceled!")
return
end
-- update progress_bar
job.percent = i / #images
-- sleep for a short moment to give stop_job callback function a chance to run
dt.control.sleep(10)
end
-- stop job and remove progress_bar from ui, but only if not alreay canceled
if(job.valid) then
job.valid = false
end
-- *************************************************
-- process depending on run option selected
-- *************************************************
if GUI.optionwidgets.cbo_run_options.selected == 1 then
rv = hugin_headless(exp_image_list,exported_images_table,img_count)
end
if GUI.optionwidgets.cbo_run_options.selected == 2 then
rv = hugin_gui(exp_image_list)
end
if GUI.optionwidgets.cbo_run_options.selected == 3 then
rv = create_hdr(exp_image_list,exported_images_table,img_count)
end
-- *************************************************
-- post processing - import, cleanup, metadata etc
-- *************************************************
-- delete exported tif files and pto file
for i,exp_file_name in pairs(exported_images_table) do
os.remove(exp_file_name)
end
if paths.pto_path ~= "" then
os.remove(paths.pto_path)
end
-- EXIF Data
exiftool = false
if GUI.optionwidgets.copy_exif.value == true then
if settings.exiftool_bin ~= '' then
-- try to copy exifdata via exiftool if reequested prior to import
local sourcefile = os_quote .. paths.firstimagepath .. os_path_seperator .. paths.firstimagename .. os_quote
local exiftool_cmd = settings.exiftool_bin .. ' -m -overwrite_original -tagsfromfile ' .. sourcefile .. ' ' .. os_quote .. paths.fullpanoname .. os_quote
dt.print_log("Running exiftool " .. exiftool_cmd)
dt.print("Copying metadata vis Exiftool")
rv = run_app('exiftool...', exiftool_cmd)
dt.print_log("Exiftool returned " .. tostring(rv))
if rv == 0 then
exiftool = true
end
end
end
-- use df.create_unique_file in case pano stacked filename already exists in source folder
local target_filename = df.create_unique_filename( paths.firstimagepath .. os_path_seperator .. paths.panoname )
dt.print_log('Source is ' .. paths.fullpanoname .. ' target is ' .. target_filename)
if target_filename ~= "" then
-- move stacked image to source folder and import
if df.file_move(paths.fullpanoname,target_filename) then
-- stacked tif now in correct folder and ready for import
dt.print_log("Moved " .. paths.fullpanoname .. " to " .. target_filename)
local imported_image = dt.database.import(target_filename)
if imported_image == nil then
dt.print_log("Unable to import " .. target_filename)
dt.print(_("Unable to move " .. paths.fullpanoname .. " to " .. paths.firstimagepath .. " please check manually"))
else
dt.print_log("Imported " .. target_filename)
-- check rotation if source images are portrait
-- group
-- first group all source images
if GUI.optionwidgets.group.value == true then
for _,imagetogroup in pairs( images_to_group ) do
imagetogroup:group_with( source_img_table[ 1 ] )
end
-- now add stacked image to group and make it leader
imported_image:group_with(source_img_table[1])
imported_image:make_group_leader()
end
-- tags
if GUI.optionwidgets.copy_tags.value == true then
insert_tags(imported_image,all_tags)
end
add_additional_tags(imported_image)
-- metadata
if GUI.optionwidgets.copy_metadata.value == true then
imported_image.title = meta_data.title
imported_image.description = meta_data.description
imported_image.creator = meta_data.creator
imported_image.rights = meta_data.rights
end
-- EXIF Data
if GUI.optionwidgets.copy_exif.value == true and not(exiftool) then
-- no exiftool so copy limited exif into imported DT database entry for pano image
-- exif of tif file will not be changed
imported_image.exif_maker = exifdata["Make"]
imported_image.exif_model = exifdata["Model"]
imported_image.exif_lens = exifdata["Lens"]
imported_image.exif_aperture = exifdata["Aperture"]
imported_image.exif_exposure = exifdata["Exposure"]
imported_image.exif_focal_length = exifdata["Focal_Length"]
imported_image.exif_iso = exifdata["ISO"]
imported_image.exif_datetime_taken = exifdata["Create_Date"]
imported_image.latitude = exifdata["Latitude"]
imported_image.longitude = exifdata["Longitude"]
imported_image.elevation = exifdata["Altitude"]
end
end
end
else
-- create_unique_filename may fail if 100 other files with same basename exist, also pernissions may cause move to fail
dt.print_log("Unable to move " .. paths.fullpanoname .. " to " .. paths.firstimagepath .. " please check manually")
dt.print(_("Unable to import " .. target_filename))
end
--- *************************************************
--- End of Function run_hugin_Process()
--- *************************************************
end
-- *************************************************
-- GUI and preferences set up, register lib
-- *************************************************
GUI.optionwidgets.group = dt.new_widget('check_button') {
label = _('group'),
value = false,
tooltip = _('group selected source images and imported result image together'),
clicked_callback = function(self)
dt.print_log( "group: "..tostring( self.value ) )
end,
reset_callback = function(self)
self.value = false
end
}
GUI.optionwidgets.copy_metadata = dt.new_widget('check_button') {
label = _('copy metadata'),
value = false,
tooltip = _('copy metadata first source image to the imported result image'),
clicked_callback = function(self)
dt.print_log( "copy metadata: "..tostring( self.value ) )
end,
reset_callback = function(self) self.value = false end
}
GUI.optionwidgets.copy_tags = dt.new_widget('check_button') {
label = _('copy tags'),
value = false,
tooltip = _('copy tags from first source image to the imported result image'),
clicked_callback = function(self)
dt.print_log( "copy tags: "..tostring( self.value ) )
end,
reset_callback = function(self) self.value = false end
}
GUI.optionwidgets.label_settings = dt.new_widget('section_label'){
label = _('settings')
}
GUI.optionwidgets.label_run_options = dt.new_widget('section_label'){
label = _('run options')
}
GUI.optionwidgets.cbo_run_options = dt.new_widget('combobox'){
label = _('run options'),
tooltip = _('Select required run option'),
editable = false,
value = 1,
"panorama",
"panorama using hugin GUI",
"tonemapped pseudo-HDR using enfuse"
}
GUI.optionwidgets.copy_exif = dt.new_widget('check_button') {
label = _('Copy EXIF'),
value = false,
tooltip = _('Copy EXIF from first image in pano group to pano image'),
clicked_callback = function(self)
dt.print_log( "Copy exif: "..tostring( self.value ) )
end,
reset_callback = function(self) self.value = false end
}
GUI.optionwidgets.label_import_options = dt.new_widget('section_label'){
label = _('import options')
}
GUI.optionwidgets.add_tags_label = dt.new_widget('label') {
label = _('new tags'),
ellipsize = 'start',
halign = 'start'
}
GUI.optionwidgets.add_tags = dt.new_widget('entry'){
tooltip = _('Additional tags to be added on import. Seperate with commas, all spaces will be removed'),
placeholder = _('Enter tags, seperated by commas'),
editable = true
}
GUI.optionwidgets.add_tags_box = dt.new_widget('box') {
orientation = 'horizontal',
GUI.optionwidgets.add_tags_label,
GUI.optionwidgets.add_tags
}
GUI.options = dt.new_widget('box') {
orientation = 'vertical',
--GUI.optionwidgets.label_run_options,
--GUI.optionwidgets.create_hdr,
--GUI.optionwidgets.use_gui,
GUI.optionwidgets.cbo_run_options,
GUI.optionwidgets.label_import_options,
GUI.optionwidgets.group,
GUI.optionwidgets.copy_metadata,
GUI.optionwidgets.copy_tags,
GUI.optionwidgets.copy_exif,
GUI.optionwidgets.add_tags_box
}
GUI.run = dt.new_widget('button'){
label = _('Process with Hugin'),
tooltip =_('Process selected images with Hugin'),
clicked_callback = function() run_hugin_Process() end
}
-- ******************************************************************
-- Preferences - locate executables for Hugin, hugin_tools, exiftools
dt.preferences.register(
mod, -- script
"exiftool_bin", -- name
"file", -- type
_('exiftool executable'), -- label
_('Select the executable for exiftool'), -- tooltip
"" -- default,
)
dt.preferences.register(
mod, -- script
"HuginStagingFolder", -- name
"directory", -- type
_('Hugin Staging Folder'), -- label
_('Select the staging folder to be used for Hugin processing'), -- tooltip
"" -- default,
)
dt.preferences.register(
mod, -- script
"hugin_Bin", -- name
"file", -- type
_('Hugin executable'), -- label
_('Select the executable for Hugin'), -- tooltip
"" -- default,
)
dt.preferences.register(
mod, -- script
"hugin_tools_directory", -- name
"directory", -- type
_('hugin_tools directory'), -- label
_('Select the directory where the hugin tools are located'), -- tooltip
"" -- default,
)
load_preferences()
local function install_module()
if not mE.module_installed then
dt.register_lib( -- register module
'HuginProcessor_Lib', -- Module name
_('Hugin Processor'), -- name
true, -- expandable
true, -- resetable
{[dt.gui.views.lighttable] = {'DT_UI_CONTAINER_PANEL_RIGHT_CENTER', 99}}, -- containers
dt.new_widget('box'){
orientation = 'vertical',
GUI.options,
GUI.run
},
nil,-- view_enter
nil -- view_leave
)
end
end
local function destroy()
dt.gui.libs["HuginProcessor_Lib"].visible = false
end
local function restart()
dt.gui.libs["HuginProcessor_Lib"].visible = true
end
if dt.gui.current_view().id == "lighttable" then -- make sure we are in lighttable view
install_module() -- register the lib
else
if not mE.event_registered then -- if we are not in lighttable view then register an event to signal when we might be
-- https://www.darktable.org/lua-api/index.html#darktable_register_event
dt.register_event(
"mdouleExample", "view-changed", -- we want to be informed when the view changes
function(event, old_view, new_view)
if new_view.name == "lighttable" and old_view.name == "darkroom" then -- if the view changes from darkroom to lighttable
install_module() -- register the lib
end
end
)
mE.event_registered = true -- keep track of whether we have an event handler installed
end
end
script_data.destroy = destroy
script_data.restart = restart
script_data.destroy_method = "hide"
script_data.show = restart
return script_data