@@ -37,7 +37,7 @@ selected images, the other button performs grouping on the entire active collect
37
37
]]
38
38
39
39
local dt = require " darktable"
40
- local mod = ' autogroup '
40
+ local MOD = ' autogrouper '
41
41
local gettext = dt .gettext
42
42
-- Tell gettext where to find the .mo file translating messages for a particular domain
43
43
gettext .bindtextdomain (" AutoGrouper" ,dt .configuration .config_dir .. " /lua/locale/" )
@@ -46,110 +46,113 @@ local function _(msgid)
46
46
end
47
47
48
48
local function InRange (test , low , high ) -- tests if test value is within range of low and high (inclusive)
49
- if test >= low and test <= high then
50
- return true
51
- else
52
- return false
53
- end
49
+ if test >= low and test <= high then
50
+ return true
51
+ else
52
+ return false
53
+ end
54
54
end
55
55
56
56
local function CompTime (first , second ) -- compares the timestamps and returns true if first was taken before second
57
- first_time = first .exif_datetime_taken
58
- if string.match (first_time , ' [0-9]' ) == nil then first_time = ' 9999:99:99 99:99:99' end
59
- first_time = tonumber (string.gsub (first_time , ' [^0-9]*' ,' ' ))
60
- second_time = second .exif_datetime_taken
61
- if string.match (second_time , ' [0-9]' ) == nil then second_time = ' 9999:99:99 99:99:99' end
62
- second_time = tonumber (string.gsub (second_time , ' [^0-9]*' ,' ' ))
63
- return first_time < second_time
57
+ first_time = first .exif_datetime_taken
58
+ if string.match (first_time , ' [0-9]' ) == nil then first_time = ' 9999:99:99 99:99:99' end
59
+ first_time = tonumber (string.gsub (first_time , ' [^0-9]*' ,' ' ))
60
+ second_time = second .exif_datetime_taken
61
+ if string.match (second_time , ' [0-9]' ) == nil then second_time = ' 9999:99:99 99:99:99' end
62
+ second_time = tonumber (string.gsub (second_time , ' [^0-9]*' ,' ' ))
63
+ return first_time < second_time
64
64
end
65
65
66
66
local function SeperateTime (str ) -- seperates the timestamp into individual components for used with OS.time operations
67
- local cleaned = string.gsub (str , ' [^%d]' ,' :' )
68
- cleaned = string.gsub (cleaned , ' ::*' ,' :' ) -- YYYY:MM:DD:hh:mm:ss
69
- local year = string.sub (cleaned ,1 ,4 )
70
- local month = string.sub (cleaned ,6 ,7 )
71
- local day = string.sub (cleaned ,9 ,10 )
72
- local hour = string.sub (cleaned ,12 ,13 )
73
- local min = string.sub (cleaned ,15 ,16 )
74
- local sec = string.sub (cleaned ,18 ,19 )
75
- return {year = year , month = month , day = day , hour = hour , min = min , sec = sec }
67
+ local cleaned = string.gsub (str , ' [^%d]' ,' :' )
68
+ cleaned = string.gsub (cleaned , ' ::*' ,' :' ) -- YYYY:MM:DD:hh:mm:ss
69
+ local year = string.sub (cleaned ,1 ,4 )
70
+ local month = string.sub (cleaned ,6 ,7 )
71
+ local day = string.sub (cleaned ,9 ,10 )
72
+ local hour = string.sub (cleaned ,12 ,13 )
73
+ local min = string.sub (cleaned ,15 ,16 )
74
+ local sec = string.sub (cleaned ,18 ,19 )
75
+ return {year = year , month = month , day = day , hour = hour , min = min , sec = sec }
76
76
end
77
77
78
78
local function GetTimeDiff (curr_image , prev_image ) -- returns the time difference (in sec.) from current image and the previous image
79
- local curr_time = SeperateTime (curr_image .exif_datetime_taken )
80
- local prev_time = SeperateTime (prev_image .exif_datetime_taken )
81
- return os.time (curr_time )- os.time (prev_time )
79
+ local curr_time = SeperateTime (curr_image .exif_datetime_taken )
80
+ local prev_time = SeperateTime (prev_image .exif_datetime_taken )
81
+ return os.time (curr_time )- os.time (prev_time )
82
82
end
83
83
84
84
local function main (on_collection )
85
- local images = {}
86
- if on_collection then
87
- local col_images = dt .collection
88
- for i ,image in ipairs (col_images ) do -- copy images to a standard table, table.sort barfs on type dt_lua_singleton_image_collection
89
- table.insert (images ,i ,image )
90
- end
91
- else
92
- images = dt .gui .selection ()
93
- end
94
- dt .preferences .write (mod , ' active_gap' , ' integer' , GUI .gap .value )
95
- if # images < 2 then return end
96
- table.sort (images , function (first , second ) return CompTime (first ,second ) end ) -- sort images by timestamp
97
-
98
- for i , image in ipairs (images ) do
99
- if i == 1 then
100
- prev_image = image
101
- elseif string.match (image .exif_datetime_taken , ' [%d]' ) ~= nil then -- make sure current image has a timestamp, if so check if it is within the user specified gap value and add to group
102
- local curr_image = image
103
- if GetTimeDiff (curr_image , prev_image ) <= GUI .gap .value then
104
- images [i ]:group_with (images [i - 1 ])
105
- end
106
- prev_image = curr_image
107
- end
108
- end
85
+ local images = {}
86
+ if on_collection then
87
+ local col_images = dt .collection
88
+ for i ,image in ipairs (col_images ) do -- copy images to a standard table, table.sort barfs on type dt_lua_singleton_image_collection
89
+ table.insert (images ,i ,image )
90
+ end
91
+ else
92
+ images = dt .gui .selection ()
93
+ end
94
+ dt .preferences .write (MOD , ' active_gap' , ' integer' , GUI .gap .value )
95
+ if # images < 2 then
96
+ dt .print (' please select at least 2 images' )
97
+ return
98
+ end
99
+ table.sort (images , function (first , second ) return CompTime (first ,second ) end ) -- sort images by timestamp
100
+
101
+ for i , image in ipairs (images ) do
102
+ if i == 1 then
103
+ prev_image = image
104
+ elseif string.match (image .exif_datetime_taken , ' [%d]' ) ~= nil then -- make sure current image has a timestamp, if so check if it is within the user specified gap value and add to group
105
+ local curr_image = image
106
+ if GetTimeDiff (curr_image , prev_image ) <= GUI .gap .value then
107
+ images [i ]:group_with (images [i - 1 ])
108
+ end
109
+ prev_image = curr_image
110
+ end
111
+ end
109
112
end
110
113
111
114
-- GUI --
112
115
GUI = {
113
- gap = {},
114
- selected = {},
115
- collection = {}
116
+ gap = {},
117
+ selected = {},
118
+ collection = {}
116
119
}
117
- temp = dt .preferences .read (mod , ' active_gap' , ' integer' )
118
- if not InRange (temp , 1 , 3600 ) then temp = 3 end
120
+ temp = dt .preferences .read (MOD , ' active_gap' , ' integer' )
121
+ if not InRange (temp , 1 , 86400 ) then temp = 3 end
119
122
GUI .gap = dt .new_widget (' slider' ){
120
- label = _ (' group gap [sec.]' ),
121
- tooltip = _ (' minimum gap, in seconds, between groups' ),
122
- soft_min = 1 ,
123
- soft_max = 30 ,
124
- hard_min = 1 ,
125
- hard_max = 3600 ,
126
- step = 1 ,
127
- digits = 0 ,
128
- value = temp ,
129
- reset_callback = function (self )
130
- self .value = 3
131
- end
123
+ label = _ (' group gap [sec.]' ),
124
+ tooltip = _ (' minimum gap, in seconds, between groups' ),
125
+ soft_min = 1 ,
126
+ soft_max = 60 ,
127
+ hard_min = 1 ,
128
+ hard_max = 86400 ,
129
+ step = 1 ,
130
+ digits = 0 ,
131
+ value = temp ,
132
+ reset_callback = function (self )
133
+ self .value = 3
134
+ end
132
135
}
133
136
GUI .selected = dt .new_widget (" button" ){
134
- label = _ (' auto group: selected' ),
135
- tooltip = _ (' auto group selected images' ),
136
- clicked_callback = function () main (false ) end
137
+ label = _ (' auto group: selected' ),
138
+ tooltip = _ (' auto group selected images' ),
139
+ clicked_callback = function () main (false ) end
137
140
}
138
141
GUI .collection = dt .new_widget (" button" ){
139
- label = _ (' auto group: collection' ),
140
- tooltip = _ (' auto group the entire collection' ),
141
- clicked_callback = function () main (true ) end
142
+ label = _ (' auto group: collection' ),
143
+ tooltip = _ (' auto group the entire collection' ),
144
+ clicked_callback = function () main (true ) end
142
145
}
143
146
dt .register_lib (
144
- " AutoGroup_Lib" , -- Module name
145
- _ ( " Auto Group " ), -- name
146
- true , -- expandable
147
- true , -- resetable
148
- {[dt .gui .views .lighttable ] = {" DT_UI_CONTAINER_PANEL_RIGHT_CENTER" , 99 }}, -- containers
149
- dt .new_widget (" box" ){
150
- orientation = " vertical" ,
151
- GUI .gap ,
152
- GUI .selected ,
153
- GUI .collection
154
- }
147
+ ' AutoGroup_Lib' , -- Module name
148
+ _ ( ' auto group ' ), -- name
149
+ true , -- expandable
150
+ true , -- resetable
151
+ {[dt .gui .views .lighttable ] = {" DT_UI_CONTAINER_PANEL_RIGHT_CENTER" , 99 }}, -- containers
152
+ dt .new_widget (" box" ){
153
+ orientation = " vertical" ,
154
+ GUI .gap ,
155
+ GUI .selected ,
156
+ GUI .collection
157
+ }
155
158
)
0 commit comments