Exporting images with lua
So far we have learned to use lua to adapt Ansel to our particular workflow. Let’s look now at how to use lua to easily export images to an online service. If you are able to upload an image to a service via the command line then you can use lua to integrate this into Ansel’s user interface.
In this next example we will use lua to export via
scp. A new storage type will appear in Ansel’s UI that will export images to a remote target via the copy mechanism in
Ansel = require "Ansel" Ansel.preferences.register("scp_export","export_path", "string","target SCP path", "Complete path to copy to. Can include user and hostname","") Ansel.register_storage("scp_export","Export via scp", function( storage, image, format, filename, number, total, high_quality, extra_data) if not Ansel.control.execute("scp "..filename.." ".. Ansel.preferences.read("scp_export", "export_path","string")) then Ansel.print_error("scp failed for "..tostring(image)) end end)
Ansel.preferences.register will add a new preference to Ansel’s preferences menu,
export_path allow us to uniquely identify our preference. These fields are reused when we read the value of the preference. The
string field tells the lua engine that the preference is a string. It could also be an integer, a filename or any of the types detailed in the API manual relating to
types_lua_pref_type . We then have the label for the preference in the preference menu, the tooltip when hovering over the value and a default value.
Ansel.register_storage is the call that actually registers a new storage type. The first argument is a name for the storage type, the second is the label that will be displayed in the UI and last is a function to call on each image. This function has a lot of parameters, but
filename is the only one we use in this example. It contains the name of a temporary file where the image was exported by Ansel’s engine.
This code will work but it has a couple of limitations. This is just a simple example after all:
We use preferences to configure the target path. It would be nicer to add an element to the export UI in Ansel. We will detail how to do that in the next section.
We do not check the returned value of
scp. That command might fail, in particular if the user has not correctly set the preference.
This script can’t read input from the user. The remote
scpmust use password-less copy –
scpcan’t be provided with a password easily, so we will leave it that way.
There is no message displayed once the example is done, only the progress bar on the lower left side tells the user that the job is complete.
coroutine.yieldto call an external program. The normal
os.executewould block other lua codes from happening.