-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuto Develop.lua
More file actions
139 lines (111 loc) · 5.03 KB
/
Auto Develop.lua
File metadata and controls
139 lines (111 loc) · 5.03 KB
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
--[[
MIT License
Copyright (c) 2024 https://github.com/shuangye/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
local LrTasks = import "LrTasks"
local catalog = import "LrApplication".activeCatalog()
local ProgressScope = import "LrProgressScope"
local LrDialogs = import "LrDialogs"
local LrLogger = import "LrLogger"
local scriptName = "Auto Develop"
-- Follow Lightroom Classic SDK Guide to see the logs
local myLogger = LrLogger("libraryLogger") -- log file name; will be in ~/Documents/LrClassicLogs/
-- myLogger:enable("logfile")
-- myLogger:enable("print")
function mylog(content)
local label = scriptName
-- print("[" .. label .. "] " .. content)
myLogger:trace("[" .. label .. "] " .. content)
end
function setDevelopSettings(settings, key, value)
local changed = false
local original = settings[key]
if original ~= value then
settings[key] = value
changed = true
end
return changed
end
-- Handles one photo
function processPhoto(photo)
local fileName = photo:getFormattedMetadata("fileName")
local format = photo:getRawMetadata("fileFormat")
if format == "VIDEO" then
return
end
local iso = photo:getRawMetadata("isoSpeedRating") -- a number
if iso == nil then
mylog(fileName .. ": unable to get ISO speed")
return
end
local changed = false
local developSettings = photo:getDevelopSettings() -- a table
local factor = 33 -- ISO <= 100: Luminance NR = 5; ISO >= 1000: Luminance NR = 30
-- Luminance noise always exists. So limit the min value.
-- Luminance noise reduction becomes less effective when > 30
local luminanceNoiseReductionMin, luminanceNoiseReductionMax = 5, 30
-- Convert to integer to prevent fake changed value in `setDevelopSettings()`
-- Seems Lightroom internal Lua interpreter does not support `//` operator: unexpected symbol near '/'
local luminanceNoiseReduction = math.floor(iso / factor)
if luminanceNoiseReduction < luminanceNoiseReductionMin then
luminanceNoiseReduction = luminanceNoiseReductionMin
end
if luminanceNoiseReduction > luminanceNoiseReductionMax then
luminanceNoiseReduction = luminanceNoiseReductionMax
end
local colorNoiseReduction = developSettings["ColorNoiseReduction"] -- number
if colorNoiseReduction < luminanceNoiseReduction then
colorNoiseReduction = luminanceNoiseReduction
end
mylog(fileName .. ": ISO = " .. iso .. ", will set luminance NR = " .. luminanceNoiseReduction .. ", color NR = " .. colorNoiseReduction)
local ret = setDevelopSettings(developSettings, "LuminanceSmoothing", luminanceNoiseReduction)
changed = changed or ret
ret = setDevelopSettings(developSettings, "ColorNoiseReduction", colorNoiseReduction)
changed = changed or ret
if changed then
catalog:withWriteAccessDo(scriptName, function(context)
photo:applyDevelopSettings(developSettings, scriptName, false)
end )
end
end
LrTasks.startAsyncTask(function()
-- If there is a selection, `catalog:getTargetPhotos()` returns the list of selected photos.
-- Otherwise, it returns the entire list of photos in the filmstrip.
-- Returning the entire list of photos in the filmstrip is not desired, so do nothing if no selection.
if catalog:getTargetPhoto() == nil then
mylog("No photos selected. Nothing to do.")
return
end
local photos = catalog:getTargetPhotos()
local count = #photos
--[[
prompt = "Will apply auto develop settings to " .. count .. " photo(s)..."
local ret = LrDialogs.confirm(scriptName, prompt)
if ret ~= "ok" then
return
end
]]
local progressScope = ProgressScope({ title = scriptName, caption = scriptName, })
for i, photo in ipairs(photos) do
processPhoto(photo)
-- LrTasks.sleep(2) -- Simulate a time-consuming operation, to check whether the ProgressScope updates correctly.
progressScope:setPortionComplete(i / count)
progressScope:setCaption("Processing " .. i .. "/" .. count)
end
progressScope:done()
end )