Hey everybody

attached is my version of the lua script to generate thumbnails. I post it
mainly to show how the DT integration works it's definitely not finished
and can be greately improved.

To install, add the file in <configdir>/lua

then create a file <configdir>/luarc with a single line in it 'require
"selection_to_pdf" '
(if you already have that file, add the line above to it

you will have a new way to export image in the pulldown menu "Export
thumbnails to pdf" and a menu where you can set the title of the generated
PDS

however there are a couple of caveats. Compared to the other versions
floating around


The good
* Integrated to DT
* Contains an actual widget to set a param that is then reused for the PDF

The bad
* based on the original perl script, so the Latex is very simple and needs
to be reworked
* no filter on export format yet, I need a list of correct image formats
for pdf to add
* only title is configurable, whereas the other scripts have way more
options
* The script generates the .tex file but not the PDF yet. That's not hard
to add, but I was lazy :)


The ugly
* only works with a git version of DT. It is possible to add a new export
storage to 1.6 but not to add menus to the UI.
* even in master, it triggers a bug that can cause DT to crash (sometime).
I know what is causing that, but it's tricky to fix. It will take a couple
of days for me to rewrite all that part...


So, have fun with that, I'd be glad to have you improve it as much as you
like, and we can add a finalized version to
https://github.com/darktable-org/lua-scripts

also, feel free to ask me any question, either here or on IRC (i'm boucman)
so we can get this thing polished and ready to go...

On Sun, May 24, 2015 at 10:25 AM, Matthias Bodenbinder <
matth...@bodenbinder.de> wrote:

> Hi,
>
> I reworked the whole script. It is more sophisticated and robust now. Main
> change is an optional resizing of the pictures to reduce the pdf file size.
> It works evry well for me
>
> Usage: do_contactsheet-test.lua [OPTION] <fullpath to jpg/png>
> Options:
>         -t|--title <txt>        Title of the document (default:
> "Contactsheet")
>         -c|--count <num>        number of pictures per line (default. 4)
>         -d|--debug              turn on debug modus (default: off).
>         -h|--help               print this usage message
>         -r|--resize             turn on resizing pictures to 150 dpi.
> (default: off)
>
> This script creates a contactsheet.pdf in the current directory. It
> depends on "lualatex" to create the pdf file.
> It expects the full path names to the pictures on the commandline.
>
> If resizing is turned on, the script resizes the thumbs to 150 dpi. This
> reduces the pdf file size dramatically.
> Example:
>         132 pictures with 20 MBit resolution (mixed landscape and portrait)
>         7 pages pdf
>         pdf size without resizing: 376 MB
>         pdf size with resizing:      5 MB
>         temp folder for the thumbs:  5 MB
>         avg. size orig. jpg's:     2.8 MB
>         avg. size thumbs:           40 kB
>         resizing took 134 seconds on a i7-2600K CPU @ 3.40GHz
>
> The script stores the resized thumbs in a temporary directory in /tmp.
> This directory is deleted on exit.
> If debug is turned on this directory is not deleted. The script depends on
> the "convert" tool from the ImageMagick package to do the resizing.
>
> Please try it out and give me some feedback.
> Matthias
>
>
> Here is the script:
> -------------------
>
> #!/usr/bin/lua
> -- do_contactsheet.lua creates a contact sheet (Kontakabzug) for a set of
> pictures
> -- it uses LaTeX to create a PDF document
> -- it supports JPG and PNG files
> -- version 1.1
> -- (c) Matthias Bodenbinder, May 2015
>
> -- default values
> title           = "Contactsheet"
> thumbs_per_line = 4
> resize          = false
> debug           = false
>
> -- width per picture = w*\textwidth
> w       = 1/thumbs_per_line - 0.01;
>
> -- pixel width per picture assuming 150 dpi
> pxl     = math.floor(20/thumbs_per_line / 2.54 * 150)
>
>
> -- LaTeX preamble
> tex_start= [[\documentclass[a4paper,10pt]{article}
> \usepackage{graphicx}
> \usepackage{fontspec}
> \usepackage{hyperref}
> \pagestyle{empty}
> \parindent0pt
> \parskip1em
> \usepackage{geometry}
> \geometry{a4paper,left=5mm,right=5mm, top=5mm, bottom=5mm}
> \begin{document}
> ]]
>
> -- place all thumbnails
> function thumbnails()
>         for i,v in ipairs(pictures) do
>                 local thumbfile = resize_image(i)
>
>                 -- mask the underscores for the labels
>                 local label = string.gsub(filenames[i],"_", "\\_")
>
>                 io.write("\\begin{minipage}[b]{" .. w .. "\\textwidth}\n"
> ..
>                 "  \\includegraphics[width=\\textwidth]{" .. thumbfile ..
> "}\\newline\n" ..
>                 "  \\centering{\\footnotesize\\href{run:" .. pictures[i]
> .. "}{\\texttt{" .. i .. ": " .. label .. "}}}\n" ..
>                 "\\end{minipage}\n")
>                 if( i % thumbs_per_line == 0 ) then
>                         -- new line
>                         io.write("\\par")
>                 else
>                         io.write("\\hspace{1mm}")
>                 end
>
>         end
> end
>
> function resize_image(i)
>         -- i = index of picture in pictures and filenames
>         fullname  = pictures[i]
>         if( not resize ) then
>                 -- if we do not resize we include the original images
>                 return(fullname)
>         end
>         smallfile = tmpdir .. "/" .. filenames[i]
>         debug_msg("% convert -resize " .. pxl .. " " .. fullname .. " " ..
> smallfile)
>         os.execute("convert -resize " .. pxl .. " " .. fullname .. " " ..
> smallfile)
>         return(smallfile)
> end
>
> function usage()
>         print("Usage: " .. arg[0] .. " [OPTION] <fullpath to jpg/png>")
>         msg = [[
> Version: 1.1
> Options:
>         -t|--title <txt>        Title of the document (default:
> "Contactsheet")
>         -c|--count <num>        number of pictures per line (default. 4)
>         -d|--debug              turn on debug modus (default: off).
>         -h|--help               print this usage message
>         -r|--resize             turn on resizing pictures to 150 dpi.
> (default: off)
>
> This script creates a contactsheet.pdf in the current directory. It
> depends on "lualatex" to create the pdf file.
> It expects the full path names to the pictures on the commandline.
>
> If resizing is turned on, the script resizes the thumbs to 150 dpi. This
> reduces the pdf file size dramatically.
> Example:
>         132 pictures with 20 MBit resolution (mixed landscape and portrait)
>         7 pages pdf
>         pdf size without resizing: 376 MB
>         pdf size with resizing:      5 MB
>         temp folder for the thumbs:  5 MB
>         avg. size orig. jpg's:     2.8 MB
>         avg. size thumbs:           40 kB
>         resizing took 134 seconds on a i7-2600K CPU @ 3.40GHz
>
> The script stores the resized thumbs in a temporary directory in /tmp.
> This directory is deleted on exit.
> If debug is turned on this directory is not deleted. The script depends on
> the "convert" tool from the ImageMagick package to do the resizing.
> ]]
>         print(msg)
> end
>
> function debug_msg(s)
>         if( debug ) then
>                 print(s)
>         end
> end
>
> -- main loop
>
> pictures = {}
> filenames = {}
> pcount = 0
> i = 1
>
> -- parse commandline
> while i <= #arg do
>         if( arg[i] == "-t" or arg[i] == "--title" ) then
>                 title = arg[i+1];
>                 i=i+1
>         elseif( arg[i] == "-c" or arg[i] == "--count" ) then
>                 thumbs_per_line = arg[i+1];
>                 w = 1/thumbs_per_line - 0.01;
>                 pxl = math.floor(20/thumbs_per_line / 2.54 * 150)
>                 i=i+1
>         elseif( arg[i] == "-r" or arg[i] == "--resize" ) then
>                 resize = true
>         elseif( arg[i] == "-h" or arg[i] == "--help" ) then
>                 usage()
>                 os.exit()
>         elseif( arg[i] == "-d" or arg[i] == "--debug" ) then
>                 debug = true
>         else
>                 table.insert(pictures, arg[i])
>         end
>         i=i+1
> end
>
> -- print global variables
>
> debug_msg("% title= " .. title)
> debug_msg("% thumbs_per_line= " .. thumbs_per_line .. "; w= " .. w)
> debug_msg("% pxl= " .. pxl)
>
> -- create random directory
> if( resize ) then
>         math.randomseed(os.time())
>         suffix = math.random(9999999)
>         tmpdir = "/tmp/lua" .. suffix
>         debug_msg("% resize= true")
>         debug_msg("% tmpdir= " .. tmpdir)
>         os.execute("mkdir -p " .. tmpdir .. " >/dev/null")
> else
>         debug_msg("% resize= false")
> end
>
> for k,v in ipairs(pictures) do
>         -- store filenames
>         -- remove leading path
>         t = string.gsub(v, ".*/", "")
>         table.insert(filenames, t)
>         debug_msg("% pic " .. k .. "= " .. v .. " = " .. t)
> end
>
> -- check for lualatex and exit if it does not exist
> if( not os.execute("which lualatex >/dev/null") )then
>         debug_msg("lualatex not found. Aborting!")
>         os.exit()
> end
> -- check for convert and exit if it does not exist
> if( resize and not os.execute("which convert >/dev/null") )then
>         print("convert not found. Please install ImageMagick. Aborting!")
>         os.exit()
> end
>
> -- write tex file to /tmp
> basefile = "/tmp/contactsheet"
> texfile = basefile .. ".tex"
> pdffile = basefile .. ".pdf"
>
> -- output routines
> f = io.output(texfile, "w")
> io.write(tex_start)
> io.write("{\\Large\\bfseries " .. title .. "}\\par\n")
> thumbnails()
> io.write("\\end{document}")
> io.close()
>
> -- excute lualatex to create pdf file
> debug_msg("% running lualatex")
> os.execute("cd /tmp; lualatex --interaction=batchmode " .. texfile .. ">
> /dev/null")
> -- move pdf to current folder
> if( not os.execute("mv " .. pdffile .. " .") ) then
>         print("Error moving pdf file: " .. pdffile)
> end
>
> -- cleanup
> if( resize and not debug ) then
>         os.execute("rm -rf " .. tmpdir)
> end
>
> -- end of script
>
>
>
>
>
> ------------------------------------------------------------------------------
> One dashboard for servers and applications across Physical-Virtual-Cloud
> Widest out-of-the-box monitoring support with 50+ applications
> Performance metrics, stats and reports that give you Actionable Insights
> Deep dive visibility with transaction tracing using APM Insight.
> http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
> _______________________________________________
> darktable-devel mailing list
> darktable-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/darktable-devel
>
--[[
  This file is part of darktable,
  copyright (c) 2015 Jérémy Rosen
  
  darktable is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  darktable is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with darktable.  If not, see <http://www.gnu.org/licenses/>.
]]
--[[
SELECTION_TO_PDF
Generates a PDF file (via Latex) containing all selected images


USAGE
* require this file from your main lua config file:

This plugin will add a new exporter that will allow you to generate the pdf file


]]
local dt = require "darktable"
dt.configuration.check_version(...,{2,0,0})

local title_widget = dt.new_widget("entry")
local widget = dt.new_widget("box") {
  orientation=horizontal,
  dt.new_widget("label"){label = "Title"},
  title_widget
}

local ending = [[
\end{document}
]]

local thumbs_per_line=4;
local width = 1/thumbs_per_line-0.01

local filename =dt.configuration.tmp_dir.."/pdfout.tex"

local function my_write(arg)
  local res,errmsg = latexfile:write(arg)
  if not res then
    dt.print(errmsg)
   error(errmsg)
   end
end
local function thumbnail(i,image,file)
  local title
  if image.title == "" then
    title = image.filename
    if image.duplicate_index > 0 then
      title = title.."["..image.duplicate_index.."]"
    end
    
  else
    title = image.title
  end
  my_write("\\begin{minipage}[b]{"..width.."\\textwidth}\n")
  my_write("\\includegraphics[width=\\textwidth]{"..file.."}\\newline\n")
  my_write("\\centering{"..i..": \\verb|"..title.."|}\n")
  my_write("\\end{minipage}\\quad\n")
end

dt.register_storage("export_pdf","Export thumbnails to pdf",
    nil,
    function(storage,image_table)
      local preamble = [[
\documentclass[a4paper,10pt]{article}
\usepackage{graphicx}
\pagestyle{empty}
\parindent0pt
\usepackage{geometry}
\geometry{a4paper,left=5mm,right=5mm, top=5mm, bottom=5mm}
\begin{document}
{\Large\bfseries ]]..title_widget.text..[[}
\bigskip\bigskip
]]

      local errmsg
      latexfile,errmsg=io.open(filename,"w")
      if not latexfile then
        dt.print(errmsg)
        error(errmsg)
      end
      my_write(preamble)
      local i = 1
      for img,file in pairs(image_table) do
         thumbnail(i,img,file)
         if i % thumbs_per_line == 0  then
            my_write("\n\\bigskip\n")
         end
         i = i+1
      end
      my_write(ending)
      latexfile:close()
    end,nil,nil,widget)

--
-- vim: shiftwidth=2 expandtab tabstop=2 cindent syntax=lua
------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud 
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
darktable-devel mailing list
darktable-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/darktable-devel

Reply via email to