|
| 1 | +--[[AutoGrouper plugin for darktable |
| 2 | +
|
| 3 | + copyright (c) 2019 Kevin Ertel |
| 4 | + |
| 5 | + darktable is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published by |
| 7 | + the Free Software Foundation, either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | + |
| 10 | + darktable is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | + |
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with darktable. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +]] |
| 18 | + |
| 19 | +--[[About this Plugin |
| 20 | +This plugin adds the module "Auto Group" to darktable's lighttable view |
| 21 | +
|
| 22 | +----REQUIRED SOFTWARE---- |
| 23 | +None |
| 24 | +
|
| 25 | +----USAGE---- |
| 26 | +Install: (see here for more detail: https://github.com/darktable-org/lua-scripts ) |
| 27 | + 1) Copy this file in to your "lua/contrib" folder where all other scripts reside. |
| 28 | + 2) Require this file in your luarc file, as with any other dt plug-in |
| 29 | +
|
| 30 | +Set a gap amount in second which will be used to determine when images should no |
| 31 | +longer be added to a group. If an image is more then the specified amount of time |
| 32 | +from the last image in the group it will not be added. Images without timestamps |
| 33 | +in exif data will be ignored. |
| 34 | +
|
| 35 | +There are two buttons. One allows the grouping to be performed only on the currently |
| 36 | +selected images, the other button performs grouping on the entire active collection |
| 37 | +]] |
| 38 | + |
| 39 | +local dt = require "darktable" |
| 40 | +local MOD = 'autogrouper' |
| 41 | +local gettext = dt.gettext |
| 42 | +-- Tell gettext where to find the .mo file translating messages for a particular domain |
| 43 | +gettext.bindtextdomain("AutoGrouper",dt.configuration.config_dir.."/lua/locale/") |
| 44 | +local function _(msgid) |
| 45 | + return gettext.dgettext("AutoGrouper", msgid) |
| 46 | +end |
| 47 | + |
| 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 |
| 54 | +end |
| 55 | + |
| 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 |
| 64 | +end |
| 65 | + |
| 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} |
| 76 | +end |
| 77 | + |
| 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) |
| 82 | +end |
| 83 | + |
| 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 |
| 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 |
| 112 | +end |
| 113 | + |
| 114 | +-- GUI -- |
| 115 | +GUI = { |
| 116 | + gap = {}, |
| 117 | + selected = {}, |
| 118 | + collection = {} |
| 119 | +} |
| 120 | +temp = dt.preferences.read(MOD, 'active_gap', 'integer') |
| 121 | +if not InRange(temp, 1, 86400) then temp = 3 end |
| 122 | +GUI.gap = dt.new_widget('slider'){ |
| 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 |
| 135 | +} |
| 136 | +GUI.selected = dt.new_widget("button"){ |
| 137 | + label = _('auto group: selected'), |
| 138 | + tooltip =_('auto group selected images'), |
| 139 | + clicked_callback = function() main(false) end |
| 140 | +} |
| 141 | +GUI.collection = dt.new_widget("button"){ |
| 142 | + label = _('auto group: collection'), |
| 143 | + tooltip =_('auto group the entire collection'), |
| 144 | + clicked_callback = function() main(true) end |
| 145 | +} |
| 146 | +dt.register_lib( |
| 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 | + } |
| 158 | +) |
0 commit comments