Windows 10 with API 9.6.0. Lua script to export selected images as 8-bit JPEG

Hi All. All my attempts to call dt.export fail because my Lua module doesn’t expose any of the ‘dt.export*’ helpers. Could someone please give me a minimal example of using ‘intermediate-export-image’ to export a few selected images to a fixed folder on Windows 10 with API 9.6.0. I have exhausted help from AI on this issue.

Hi @alan-t-L,

Welcome to darktable! I am wondering, to better understand you, what are you trying to accomplish with the LUA script? Since export to jpeg 8 bit is in the de export module of darktabel.

local my_fixed_directory = "some/place/to/put/my/exported/images"

local exporter = dt.new_format("jpg")
exporter.bpp = 8
exporter.quality = 90

for _, image in ipairs(images) do
  exporter:write_image(image, my_fixed_directory .. "/" .. image.filename, false)
end

Hi Martinus. Thanks for replying. Here’s a better explanation of my problem but also take note of my reply further below to wpferguson who provided some example code that I have tried - I still have some problems…

I am having a problem exporting images because, in my darktable 5.4.1 / Lua API 9.6.0 build on Windows 10, there is no direct dt.export function in the Lua API, and the library also doesn’t support the CLI‑level --library option that have I also tried with darktable-cli. So all of the “simple” export approaches I have tried so far fail inside a running darktable:

Calling dt.export (or variants like dt.export_image) fails because those fields are nil in my darktable Lua module.
Calling darktable-cli from Lua fails because it tries to open the main database, sees the lock held by the GUI, and aborts.

The supported mechanism, according to the current Lua API docs, is the intermediate-export-image event:

I trigger exports by registering a handler for intermediate-export-image and scheduling export jobs that darktable processes through its normal pipeline.

Darktable then calls my handler with the final output filename, which is exactly what an external program needs (a set of JPEGs in a fixed folder for further processing).

So that is why have I asked for a minimal working example of using intermediate- export-image to export a few selected images to a fixed folder on Windows with API 9.6.0. That would avoid any use of dt.export or darktable-cli and it would also use the documented event‑driven export path that matches the capabilities of your Lua API version.

I guess I could have phased my request better. What I really need is a minimal example that:

  • Takes the current selection
  • Exports each selected image as an 8‑bit JPEG
  • Writes them into a fixed folder (e.g. C:\Users.…\darktable_exports_for_proglets),
  • And logs or returns the final filenames?

I would like to reuse the example script inside a Lua script that needs to batch‑export images for external processing.

I have a series of tools that I have written in Python (I call these tools “proglets”), that perform various image processing tasks. My ultimate aim is to have a darktable Lua script that can export a selection of images as 8-bit JPEGs to a designated folder then calls the my external “image processing proglet” with a parameter that points to the folder containing the exported images. The proglet does its processing on those images and outputs a final image (usually a 16-bit TIFF) to a designated folder where the dt Lua script then imports the resulting “proglet” output.

Hi Bill, thanks for your example lua code. Much appreciated.

I have incorperated your example into my lua script but it also fails for me.

I also used a debug probe to find out the formats availabe to me:

– Debug: list available export format names
local ok_fmt, dt_format = pcall(function() return dt.format end)
if ok_fmt and dt_format then
dt.print_error(“Lighten proglet: available formats:”)
for name, _ in pairs(dt_format) do
dt.print_error(" format key: " … tostring(name))
end
else
dt.print_error(“Lighten proglet: dt.format not accessible”)
end

On darktable 5.4.1 with Lua API 9.6.0 on Windows, the darktable Lua module I get from require “darktable” doesn’t expose dt.export, dt.export_image or dt.format. Trying to call darktable.new_format(“jpg”) from Lua fails with field “jpg” not found for type dt_lua_singleton_format. In other words, the usual fmt = dt.new_format(“jpg”); fmt:write_image(…) pattern isn’t available in my environment, even though darktable-cli and the GUI can export JPEGs just fine.

I know understand your question much better. But my question is, why do you want to use a lua script from this? what is the idea behind this? I am asking because what you want can al be done very easy through a preset in export module in the lighttable overview.

I do understand that you want to use it for external processing. But wouldn’t it be easier to use push the export button and then call the python script yourself?

If you go to GitHub - wpferguson/extra-dt-lua-scripts: Extra lua scripts for darktable that can't or wont be included in the distributed darktable-org/lua-scripts · GitHub, there is a script called postsharpen.lua. It exports a set of images and then sharpens the exported images and places them where the user wants.

At the end of the script is the register_storage which creates an entry in the exporter for the user to use. The functions to look at would be show_status, sharpen, and setup which would show you how I did it.

Much appreciated Bill, thanks! I’ll take a good look at it.

Yes, I genuinely agree with you about that. The “proglets” are a series of small, bespoke image tools written over period of time while learning python. They work like standalone programs or can be piped to each other, input and output, used to process Astro and other photos but are not mine to share. The darktable export > python “proglet” output > darktable import was requested by a friend who has moved to using dt for some image post-processing like image sequences instead of Adobe Lightroom that has become too expensive. I was asked to see if I could automate the darktable export > python “proglet” output > darktable import workflow or at least to make it easier for my friend. I’m sure an expensive AI could do the job but we both thought a litle help from AI and a Lua script might do it too.

Hi Bill. Your postsharpen.lua script with its own storage-based export mechanism really did provide what I needed and I now have a working lua script myself. With my lua script, I can now export selected images - after setting the target storage to “proglet-a” or “proglet-b” (the bespoke external image processing programs, written in python) and setting the export file format to JPEG 8-bit. The images are then exported to a designated folder. A JSON job file is created listing the exported images. An external OS wrapper is then called to run the chosen proglet on the exported images using the JSON job file. The resulting proglet output is then placed in a predefined taget folder. Once the external proglet has finishes, the Lua script brings the resulting image back into darktable. After this import, darktable creates the XMP sidecar for the imported image and updates its database to include it in a film roll corresponding to the proglet output directory. Excellent, I couldn’t have done it without your help.

1 Like