Last active
August 29, 2015 14:20
-
-
Save bbinet/ba68c10cddf72d4dd436 to your computer and use it in GitHub Desktop.
Darktable plugin to normalize directory path of imported images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
Normalize Import | |
A simple plugin to normalize directory path (film) of imported images. | |
When a new image is imported, the plugin immediately moves the new image to the | |
following normalized directory path: | |
<photos_path>/YYYY/MM/DD/<image.jpg> | |
AUTHOR | |
Bruno Binet <[email protected]> | |
INSTALLATION | |
* copy this file in $CONFIGDIR/lua/ where CONFIGDIR is your darktable | |
configuration directory | |
* add the following line in the file $CONFIGDIR/luarc require | |
"normalize-import" | |
USAGE | |
* set the <photos_path> directory for storing normalized imported photos in | |
preferences => lua => Photos path | |
* import your images as you usually do | |
* they are automatically moved to the <photos_path> directory | |
LICENSE | |
MIT | |
]] | |
local dt = require "darktable" | |
local lfs = require "lfs" | |
function pathchunks(name) | |
local chunks = {} | |
for w in string.gmatch(name, "[^/\\]+") do | |
table.insert(chunks, 1, w) | |
end | |
return chunks | |
end | |
function mkdir_p(path) | |
local chunks = pathchunks(path) | |
local originalpath = lfs.currentdir() | |
lfs.chdir("/") | |
for i=#chunks, 1, -1 do | |
local c = chunks[i] | |
local exists = lfs.attributes(c) ~= nil | |
if(not exists) then | |
lfs.mkdir(c) | |
end | |
lfs.chdir(c) | |
end | |
lfs.chdir(originalpath) | |
return path | |
end | |
local function move_image(event, image) | |
local Y, m, d, H, M, S = string.match( | |
image.exif_datetime_taken, "(%d+):(%d+):(%d+) (%d+):(%d+):(%d+)") | |
local lpath = table.concat({Y, m, d,}, '/') | |
local photos_path = dt.preferences.read("normalize-import", "photos_path", "directory") | |
local abspath = photos_path .. '/' .. lpath | |
mkdir_p(abspath) | |
local old_film = image.film | |
local new_film = dt.films.new(abspath) | |
image.move(new_film, image) | |
-- print(tostring(image) .. ' ==> ' .. tostring(moved_image)) | |
if (image.film == old_film) then | |
-- image.move has failed | |
image.delete(image) | |
end | |
old_film.delete(old_film) | |
end | |
dt.preferences.register("normalize-import", "photos_path", | |
"directory", "Base path of the photos collection", | |
"Photos will automatically be imported in this directory", | |
"~/Photos") | |
dt.register_event("post-import-image", move_image) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment