Lua script: batch automatic perspective correction

This Lua script runs the automatic perspective correction on every selected image, one after another, similar to Lightroom’s Auto transform. It presses the “structure > auto” and “fit > both” buttons of the “rotate and perspective” module for each image.

Tested on darktable 5.6.2 (Windows) with a few images. Please test on a copy first.

Requirements:

  • Put the file in the darktable config lua folder (on Windows: %LOCALAPPDATA%\darktable\lua)
  • Add this line to the luarc file in the config folder: require “perspektif_toplu”
  • Open “rotate and perspective” in the darkroom once so the module is visible
  • Select images in lighttable, then use the shortcut assigned under Lua Scripts > “perspektif toplu” (Preferences > shortcuts)
  • Do not touch mouse or keyboard while it runs

Limitations: works only when the image has enough detectable lines. Adjust the wait times if your machine is slow.

local dt = require "darktable"

local BEKLE_ACILIS  = 1500
local BEKLE_ANALIZ  = 2500
local BEKLE_FIT     = 1000

local function bekle(ms) dt.control.sleep(ms) end

local function bir_fotograf(img)
  dt.gui.views.darkroom.display_image(img)
  bekle(BEKLE_ACILIS)
  dt.gui.action("iop/ashift", 0, "focus", "on", 1.0)
  bekle(500)
  dt.gui.action("iop/ashift/structure/auto", 0, "", "", 1.0)
  bekle(BEKLE_ANALIZ)
  dt.gui.action("iop/ashift/fit/both", 0, "", "", 1.0)
  bekle(BEKLE_FIT)
  dt.gui.action("iop/ashift", 0, "focus", "off", 1.0)
  bekle(500)
end

local function toplu()
  local liste = {}
  for _, img in ipairs(dt.gui.action_images) do liste[#liste + 1] = img end
  if #liste == 0 then
    dt.print("No image selected")
    return
  end
  dt.gui.current_view(dt.gui.views.darkroom)
  bekle(1500)
  for i, img in ipairs(liste) do
    dt.print(string.format("Perspective: %d / %d", i, #liste))
    local ok, hata = pcall(bir_fotograf, img)
    if not ok then dt.print("Error: " .. tostring(hata)) end
  end
  dt.print("Perspective batch finished")
end

dt.register_event("perspektif_toplu", "shortcut",
  function() toplu() end, "perspektif toplu")