From 6e5dbc0b83584679d8005cb3fed74a0f08281b65 Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Fri, 15 Apr 2016 00:38:00 -0400 Subject: [PATCH] Incorporated supertobi's suggestions. Replaced file and path splitting code with a regular expression (thanks Tobias Jakobs) to ensure filenames were handled correctly. Used the image data in the image table pairs instead of darktable.gui.action_images to ensure there were no mismatches as well as clean up the code nicely. Updated gimp.po to reflect the line number changes due to code refactoring. --- contrib/de_DE/LC_MESSAGES/gimp.po | 8 +-- contrib/gimp.lua | 95 ++++++++++++------------------- 2 files changed, 39 insertions(+), 64 deletions(-) diff --git a/contrib/de_DE/LC_MESSAGES/gimp.po b/contrib/de_DE/LC_MESSAGES/gimp.po index b3b0447..3df09d1 100644 --- a/contrib/de_DE/LC_MESSAGES/gimp.po +++ b/contrib/de_DE/LC_MESSAGES/gimp.po @@ -21,19 +21,19 @@ msgstr "" "dngettext:2,3\n" "X-Poedit-Basepath: .\n" -#: gimp.lua:123 +#: gimp.lua:96 #, lua-format msgid "Export Image %i/%i" msgstr "Exportiere Bild %i/%i" -#: gimp.lua:129 +#: gimp.lua:101 msgid "GIMP not found" msgstr "GIMP nicht gefunden" -#: gimp.lua:143 +#: gimp.lua:115 msgid "Launching GIMP..." msgstr "Starten von GIMP" -#: gimp.lua:182 +#: gimp.lua:150 msgid "Edit with GIMP" msgstr "Bearbeiten mit Gimp" diff --git a/contrib/gimp.lua b/contrib/gimp.lua index b546e70..24be479 100644 --- a/contrib/gimp.lua +++ b/contrib/gimp.lua @@ -1,6 +1,6 @@ --[[ - gimp.lua - export and edit with gimp + gimp.lua - export and edit with GIMP Copyright (C) 2016 Bill Ferguson . @@ -25,32 +25,32 @@ along with this program. If not, see . ]] --[[ - gimp - export an image and open with gimp for editing + gimp - export an image and open with GIMP for editing This script provides another storage (export target) for darktable. Selected - images are exported in the specified format to temporary storage. Gimp is launched + images are exported in the specified format to temporary storage. GIMP is launched and opens the files. After editing, the exported images are overwritten to save the - changes. When gimp exits, the exported files are moved into the current collection + changes. When GIMP exits, the exported files are moved into the current collection and imported into the database. The imported files then show up grouped with the originally selected images. ADDITIONAL SOFTWARE NEEDED FOR THIS SCRIPT - * gimp - http://www.gimp.org + * GIMP - http://www.gimp.org USAGE * require this script from your main lua file - * select an image or images for editing with gimp - * in the export dialog select "Edit with gimp" and select the format and bit depth for the + * select an image or images for editing with GIMP + * in the export dialog select "Edit with GIMP" and select the format and bit depth for the exported image * Press "export" - * Edit the image with gimp then save the changes with File->Overwrite.... - * Exit gimp + * Edit the image with GIMP then save the changes with File->Overwrite.... + * Exit GIMP * The edited image will be imported and grouped with the original image CAVEATS - * Developed and tested on Ubuntu 14.04 LTS with darktable 2.0.3 and gimp 2.9.3 (development version with + * Developed and tested on Ubuntu 14.04 LTS with darktable 2.0.3 and GIMP 2.9.3 (development version with > 8 bit color) - * There is no provision for dealing with the xcf files generated by gimp, since darktable doesn't deal with + * There is no provision for dealing with the xcf files generated by GIMP, since darktable doesn't deal with them. You may want to save the xcf file if you intend on doing further edits to the image or need to save the layers used. Where you save them is up to you. @@ -66,37 +66,10 @@ dt.configuration.check_version(...,{3,0,0}) -- Tell gettext where to find the .mo file translating messages for a particular domain gettext.bindtextdomain("gimp",dt.configuration.config_dir.."/lua/") --- Thanks to http://lua-users.org/wiki/SplitJoin for the split and split_path functions -local function split(str, pat) - local t = {} -- NOTE: use {n = 0} in Lua-5.0 - local fpat = "(.-)" .. pat - local last_end = 1 - local s, e, cap = str:find(fpat, 1) - while s do - if s ~= 1 or cap ~= "" then - table.insert(t,cap) - end - last_end = e+1 - s, e, cap = str:find(fpat, last_end) - end - if last_end <= #str then - cap = str:sub(last_end) - table.insert(t, cap) - end - return t -end - -local function split_path(str) - return split(str,'[\\/]+') -end - local function get_filename(str) - parts = split_path(str) - return parts[#parts] -end - -local function basename(str) - return string.sub(str,1,-4) + -- Thanks to Tobias Jakobs for the awesome regular expression + local path, filename, filetype = string.match(str, "(.-)([^\\/]-%.?([^%.\\/]*))$") + return filename end local function _(msgid) @@ -125,7 +98,7 @@ end local function gimp_edit(storage, image_table, extra_data) --finalize if not checkIfBinExists("gimp") then - dt.print_error(_("gimp not found")) + dt.print_error(_("GIMP not found")) return end @@ -139,7 +112,7 @@ local function gimp_edit(storage, image_table, extra_data) --finalize img_list = img_list ..exp_img.. " " end - dt.print(_("Launching gimp...")) + dt.print(_("Launching GIMP...")) local gimpStartCommand gimpStartCommand = "gimp "..img_list @@ -148,23 +121,25 @@ local function gimp_edit(storage, image_table, extra_data) --finalize coroutine.yield("RUN_COMMAND", gimpStartCommand) - -- for each of the exported images - -- find the matching original image - -- then move the exported image into the directory with the original - -- then import the image into the database which will group it with the original - -- and then copy over any tags other than darktable tags + -- for each of the image, exported image pairs + -- move the exported image into the directory with the original + -- then import the image into the database which will group it with the original + -- and then copy over any tags other than darktable tags - for _,exp_img in pairs(image_table) do - local fname = get_filename(exp_img) - for _,orig_img in pairs(dt.gui.action_images) do - if basename(fname) == basename(orig_img.filename) then - os.execute("mv "..exp_img.." "..orig_img.path) - local myimage = dt.database.import(orig_img.path.."/"..fname) - for _,tag in pairs(dt.tags.get_tags(orig_img)) do - if not (string.sub(tag.name,1,9) == "darktable") then - dt.tags.attach(tag,myimage) - end - end + for image,exported_image in pairs(image_table) do + + local myimage_name = image.path .. "/" .. get_filename(exported_image) + + dt.print_error("moving " .. exported_image .. " to " .. myimage_name) + result = os.rename(exported_image, myimage_name) + + dt.print_error("importing file") + local myimage = dt.database.import(myimage_name) + + for _,tag in pairs(dt.tags.get_tags(image)) do + if not (string.sub(tag.name,1,9) == "darktable") then + dt.print_error("attaching tag") + dt.tags.attach(tag,myimage) end end end @@ -172,7 +147,7 @@ local function gimp_edit(storage, image_table, extra_data) --finalize end -- Register -dt.register_storage("module_gimp", _("Edit with gimp"), show_status, gimp_edit) +dt.register_storage("module_gimp", _("Edit with GIMP"), show_status, gimp_edit) --