Skip to content

Commit 2c3536a

Browse files
authored
Changes requestd via PR
1) mod changed to MOD 2) autogroup changed to autogrouper 3) prints "please select at least 2 images" if user attempts to run with fewer than 2 images selected 4) change visible module name to "auto group" (lower case) 5) changed soft max on slider to 60s - I also increase the hard max up to a full day, I saw no reason not to
1 parent 9e97b8f commit 2c3536a

File tree

1 file changed

+86
-83
lines changed

1 file changed

+86
-83
lines changed

contrib/AutoGrouper.lua

Lines changed: 86 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ selected images, the other button performs grouping on the entire active collect
3737
]]
3838

3939
local dt = require "darktable"
40-
local mod = 'autogroup'
40+
local MOD = 'autogrouper'
4141
local gettext = dt.gettext
4242
-- Tell gettext where to find the .mo file translating messages for a particular domain
4343
gettext.bindtextdomain("AutoGrouper",dt.configuration.config_dir.."/lua/locale/")
@@ -46,110 +46,113 @@ local function _(msgid)
4646
end
4747

4848
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
5454
end
5555

5656
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
6464
end
6565

6666
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}
7676
end
7777

7878
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)
8282
end
8383

8484
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
109112
end
110113

111114
-- GUI --
112115
GUI = {
113-
gap = {},
114-
selected = {},
115-
collection = {}
116+
gap = {},
117+
selected = {},
118+
collection = {}
116119
}
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
119122
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
132135
}
133136
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
137140
}
138141
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
142145
}
143146
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+
}
155158
)

0 commit comments

Comments
 (0)