Failing to duplicate image in LUA

Hi all,
I am running bleeding edge darktable and trying to automate some procedures like mailing an image to my blog.
With this task I got stuck trying to duplicate an image via lua (Background: I want to “freeze” the published version, and with the lack of “lock” or “freeze” in dt, the best solution is to tag the published version accordingly and create a duplicate as a basis for further processing)
This is what I got so far:

              dt.gui.current_view(dt.gui.views.darkroom)
              dt.gui.views.darkroom.display_image( i )
              dt.control.sleep(1000)
              dt.gui.action("lib/duplicate", 0, "", "", 1.0)
              -- dt.gui.current_view(dt.gui.views.lighttable) 

Unfortunately the dt.gui.action() has no effect. Earlier attempts with

local newi = dt.database.duplicate( i )

were fruitless, too, as I found no way to copy the history stack to the new image.

Any idea would be highly appreciated!

Best regards - Markus

You could try

local newimg = dt.database.duplicate(oldimg)
local tmpstyle = dt.styles.create(oldimg)
dt.styles.apply(tmpstyle, newimg)
dt.styles.delete(tmpstyle)

Thanks, Bill. I’ll try this.

Hi,
I’ve tried the way as wpferguson mentioned, but the duplicated images will be weird, the modules were triple times than in the original one. In styles I set the mode to overwrite.

local selectedImages = dt.gui.selection()
if selectedImages then
  for _, image in pairs(selectedImages) do
	
		local newimg = dt.database.duplicate(image)
		local tmpstyle = dt.styles.create(image)
		dt.styles.apply(tmpstyle, newimg)
		dt.styles.delete(tmpstyle)

  end
end

I’ve tried to replace the duplicate function with this, but this resulted to fail to load the script.

local newimg = dt.database.duplicate_image_with_history(image)

Have you any idea? Thank you

Are you on 4.8.x? That was the first release that had that function available.

You could also use the syntax

local newimg = image:duplicate_image_with_history()

I finally succeeded in 4.8.1 with that syntax:

      for i_, i in ipairs(dt.gui.action_images) do
            local newimg = i.duplicate_with_history(i)

This is not the syntay I would assume from the documentation, but it works…

The other syntax should work, but I just looked and the entry isn’t there for it to. I’ll fix it.

PR submitted

Hi,
When I wrote my first answer I had version 4.6.x, I just updated to the latest 4.8.1, but it still failed to load even with local newimg = image:duplicate_image_with_history().

So I’ve tried what springm wrote, so my working code is this:
Thanks both of you for helping.

local dt = require "darktable"

local MODULE = "my_duplicate_script"

local selectedImages = dt.gui.selection()
if selectedImages then
  for _, image in pairs(selectedImages) do
		local newimg = image.duplicate_with_history(image)
  end
end

local function destroy()
  darktable.destroy_event(MODULE .. "_my_duplicate_script", "shortcut")
end
darktable.register_event(MODULE .. "_my_duplicate_script", "shortcut",
  function(event, shortcut)
  end, "my_duplicate_script"
)
local script_data = {}
script_data.destroy = destroy
return script_data
local newimg = image.duplicate_with_history(image)

should be

 local newimg = image:duplicate_with_history()

the duplicate_with_history() function is in API 9.4.0, which is darktable 5.0, which will be released in ~3 weeks

1 Like