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

Reply via email to