-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathcreate_thumbnails.lua
140 lines (108 loc) · 5.27 KB
/
create_thumbnails.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
--[[
Create thumbnails plugin for darktable
darktable 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.
darktable 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 darktable. If not, see <http://www.gnu.org/licenses/>.
]]
--[[About this Plugin
This plugin adds the buttons 'create thumbnails' and 'delete thumbnails' to 'selected image[s]' module of darktable's lighttable view.
----USAGE----
Click the 'create thumbnails' button in the 'selected image[s]' module to let the script create full sized previews of all selected images.
Click the 'delete thumbnails' button in the 'selected image[s]' module to let the script delete all previews/thumbnails of all selected images.
To create (or delete) previews of all images of a collection:
Use CTRL+A to select all images of current collection and then press the corresponding button.
]]
local dt = require "darktable"
local du = require "lib/dtutils"
du.check_min_api_version("8.0.0", "create_thumbnails_button")
-- stop running thumbnail creation
local function stop_job(job)
job.valid = false
end
-- return "1 image" if amount_of_images is 1 else "<amount_of_images> images"
local function image_count_to_text(amount_of_images)
return amount_of_images .. " image" .. (amount_of_images == 1 and "" or "s")
end
-- print the given message to the user and log the given message
local function print_and_log(message)
dt.print(message)
dt.print_log(message)
end
-- add button to 'selected images' module
dt.gui.libs.image.register_action(
"create_thumbnails_button",
"create thumbnails",
function(event, images)
print_and_log("creating thumbnails for " .. image_count_to_text(#images) .. " ...")
-- create a new progress_bar displayed in darktable.gui.libs.backgroundjobs
job = dt.gui.create_job("creating thumbnails...", true, stop_job)
for i, image in pairs(images) do
-- generate all thumbnails, a max value of 8 means that also a full size preview image is created
-- check only once if the mipmap cache directories exist
image:generate_cache(i == 1, 0, 8)
-- 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)
-- stop early if darktable is shutdown or the cancle button of the progress bar is pressed
if dt.control.ending or not job.valid then
print_and_log("creating thumbnails canceled after processing " .. i .. "/" .. image_count_to_text(#images) .. "!")
break
end
end
-- if job was not canceled
if(job.valid) then
-- stop job and remove progress_bar from ui
job.valid = false
print_and_log("creating thumbnails for " .. image_count_to_text(#images) .. " done!")
end
end,
"create full sized previews of all selected images"
)
-- add button to 'selected images' module
dt.gui.libs.image.register_action(
"delete_thumbnails_button",
"delete thumbnails",
function(event, images)
print_and_log("deleting thumbnails of " .. image_count_to_text(#images) .. " ...")
-- create a new progress_bar displayed in darktable.gui.libs.backgroundjobs
job = dt.gui.create_job("deleting thumbnails...", true, stop_job)
for i, image in pairs(images) do
-- delete all thumbnails
image:drop_cache()
-- 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)
-- stop early if darktable is shutdown or the cancle button of the progress bar is pressed
if dt.control.ending or not job.valid then
print_and_log("deleting thumbnails canceled after processing " .. i .. "/" .. image_count_to_text(#images) .. "!")
break
end
end
-- if job was not canceled
if(job.valid) then
-- stop job and remove progress_bar from ui
job.valid = false
print_and_log("deleting thumbnails of " .. image_count_to_text(#images) .. " done!")
end
end,
"delete all thumbnails of all selected images"
)
-- clean up function that is called when this module is getting disabled
local function destroy()
dt.gui.libs.image.destroy_action("create_thumbnails_button")
dt.gui.libs.image.destroy_action("delete_thumbnails_button")
end
local script_data = {}
script_data.destroy = destroy -- function to destory the script
script_data.destroy_method = nil -- set to hide for libs since we can't destroy them commpletely yet
script_data.restart = nil -- how to restart the (lib) script after it's been hidden - i.e. make it visible again
return script_data