|
| 1 | +--[[ |
| 2 | +Autostyle |
| 3 | +Automatically apply a given style when an exif tag is present in the file. This tagged is checked with exiftool, in order to be able to match very exotic tags. |
| 4 | +I wrote this initially to be able to apply a style to compensate for Auto-DR from my Fujifilm camera |
| 5 | +
|
| 6 | +AUTHOR |
| 7 | + |
| 8 | +
|
| 9 | +INSTALATION |
| 10 | +* copy this file in $CONFIGDIR/lua/ where CONFIGDIR |
| 11 | +is your darktable configuration directory |
| 12 | +* add the following line in the file $CONFIGDIR/luarc |
| 13 | + require "autostyle" |
| 14 | +
|
| 15 | +USAGE |
| 16 | +* set the exif configuration string in your lua configuration |
| 17 | + mine, for instance, is AutoDynamicRange=200%=>DR200" |
| 18 | + meaning that I automatically want to apply my DR200 style on all |
| 19 | + images where exiftool returns '200%' for the AutoDynamicRange tag |
| 20 | +* if you want to be able to apply it manually to already imported |
| 21 | + images, define a shortcut (lua shortcuts). As I couldn't find an event for |
| 22 | + when a development is removed, so the autostyle won't be applied again, |
| 23 | + this shortcut is also helpful then |
| 24 | +* import your images, or use the shortcut on your already imported images |
| 25 | +* To determine which tag you want, list all tags with exiftool: |
| 26 | + exiftool -j XE021351.RAF, and find the one you want to use |
| 27 | + you can check it with |
| 28 | + > exiftool -AutoDynamicRange XE021351.RAF |
| 29 | + Auto Dynamic Range : 200% |
| 30 | +
|
| 31 | +
|
| 32 | +LICENSE |
| 33 | +GPLv2 |
| 34 | +
|
| 35 | +]] |
| 36 | + |
| 37 | +local darktable = require "darktable" |
| 38 | + |
| 39 | + |
| 40 | +-- Forward declare the functions |
| 41 | +local autostyle_apply_one_image,autostyle_apply_one_image_event,autostyle_apply,exiftool_attribute,capture |
| 42 | + |
| 43 | +-- Tested it with darktable 1.6.1 and darktable git from 2014-01-25 |
| 44 | +darktable.configuration.check_version(...,{2,0,2},{2,1,0}) |
| 45 | + |
| 46 | +-- Receive the event triggered |
| 47 | +function autostyle_apply_one_image_event(event,image) |
| 48 | + autostyle_apply_one_image(image) |
| 49 | +end |
| 50 | + |
| 51 | +-- Apply the style to an image, if it matches the tag condition |
| 52 | +function autostyle_apply_one_image (image) |
| 53 | + -- We need the tag, the value and the style_name provided from the configuration string |
| 54 | + local tag,value,style_name=string.match(darktable.preferences.read("autostyle","exif_tag","string"),"(%g+)%s*=%s*(%g+)%s*=>%s*(%g+)") |
| 55 | + |
| 56 | + -- check they all exist (correct syntax) |
| 57 | + if (not tag) then |
| 58 | + darktable.print("EXIF TAG not found in " .. darktable.preferences.read("autostyle","exif_tag","string")) |
| 59 | + return |
| 60 | + end |
| 61 | + if (not value) then |
| 62 | + darktable.print("value to match not found in " .. darktable.preferences.read("autostyle","exif_tag","string")) |
| 63 | + return |
| 64 | + end |
| 65 | + if (not style_name) then |
| 66 | + darktable.print("style name not found in " .. darktable.preferences.read("autostyle","exif_tag","string")) |
| 67 | + return |
| 68 | + end |
| 69 | + |
| 70 | + -- First find the style (we have its name) |
| 71 | + local styles= darktable.styles |
| 72 | + local style |
| 73 | + for _,s in ipairs(styles) do |
| 74 | + if s.name == style_name then |
| 75 | + style=s |
| 76 | + end |
| 77 | + end |
| 78 | + if (not style) then |
| 79 | + darktable.print("style not found for autostyle: " .. style_name) |
| 80 | + end |
| 81 | + |
| 82 | + -- Apply the style to image, if it is tagged |
| 83 | + local ok,auto_dr_attr= pcall(exiftool_attribute,image.path .. '/' .. image.filename,tag) |
| 84 | + -- If the lookup fails, stop here |
| 85 | + if (not ok) then |
| 86 | + return |
| 87 | + end |
| 88 | + if auto_dr_attr==value then |
| 89 | +-- darktable.print("Image " .. image.filename .. ": autostyle automatically applied " .. darktable.preferences.read("autostyle","exif_tag","string") ) |
| 90 | + darktable.styles.apply(style,image) |
| 91 | +-- else |
| 92 | +-- darktable.print("Image " .. image.filename .. ": autostyle not applied, exif tag " .. darktable.preferences.read("autostyle","exif_tag","string") .. " not matched: " .. auto_dr_attr) |
| 93 | + end |
| 94 | +end |
| 95 | + |
| 96 | + |
| 97 | +function autostyle_apply( shortcut) |
| 98 | + local images = darktable.gui.action_images |
| 99 | + for _,image in pairs(images) do |
| 100 | + autostyle_apply_one_image(image) |
| 101 | + end |
| 102 | +end |
| 103 | + |
| 104 | +-- Retrieve the attribute through exiftool |
| 105 | +function exiftool_attribute(path,attr) |
| 106 | + local cmd="exiftool -" .. attr .. " '" ..path.. "'"; |
| 107 | + local exifresult=get_stdout(cmd) |
| 108 | + local attribute=string.match(exifresult,": (.*)") |
| 109 | + if (attribute == nil) then |
| 110 | + darktable.print( "Could not find the attribute " .. attr .. " using the command: <" .. cmd .. ">") |
| 111 | + -- Raise an error to the caller |
| 112 | + error( "Could not find the attribute " .. attr .. " using the command: <" .. cmd .. ">"); |
| 113 | + end |
| 114 | + return attribute |
| 115 | +end |
| 116 | + |
| 117 | +-- run command and retrieve stdout |
| 118 | +function get_stdout(cmd) |
| 119 | + -- Open the command, for reading |
| 120 | + local fd = assert(io.popen(cmd, 'r')) |
| 121 | + -- yield to other lua threads until data is ready to be read |
| 122 | + coroutine.yield("FILE_READABLE",fd) |
| 123 | + -- slurp the whole file |
| 124 | + local data = assert(fd:read('*a')) |
| 125 | + |
| 126 | + fd:close() |
| 127 | + -- Replace carriage returns and linefeeds with spaces |
| 128 | + data = string.gsub(data, '[\n\r]+', ' ') |
| 129 | + -- Remove spaces at the beginning |
| 130 | + data = string.gsub(data, '^%s+', '') |
| 131 | + -- Remove spaces at the end |
| 132 | + data = string.gsub(data, '%s+$', '') |
| 133 | + return data |
| 134 | +end |
| 135 | + |
| 136 | +-- Registering events |
| 137 | +darktable.register_event("shortcut",autostyle_apply, |
| 138 | + "Apply your chosen style from exiftool tags") |
| 139 | + |
| 140 | +darktable.preferences.register("autostyle","exif_tag","string","Autostyle: EXIF_tag=value=>style","apply a style automatically if an EXIF_tag matches value. Find the tag with exiftool","") |
| 141 | + |
| 142 | +darktable.register_event("post-import-image",autostyle_apply_one_image_event) |
| 143 | + |
| 144 | + |
0 commit comments