J-S J-S - 1 year ago 76
Linux Question

Linux, combine images to precise scaled tiled pdf. Imagemagick? Alternative?

I have a large collection of jpeg images with the same aspect ratio but not necessarily the same resolution. I need to combine those into one (or multiple) pdf documents with multiple images showing up per page.

I need to specify a target page size (a4/letter) and I need precise control over the final size of the combined images.

The scenario is something like "combining a list of passport photos for print".

I tried montage from Imagemagick but I cannot get the scaling done. If all images had exactly the same resolution I could try to find some voodoo numbers and make it work somehow.

Maybe imagemagick is the wrong approach? Is there a way to create a pdf template with x:y "slots" that already have the correct size for print and then "feed" the images into those slots?

Answer Source

I've done thing kind of thing with pdflatex. You write a tiny script in Python/Ruby/bash/whatever to walk your directory finding all the image files, then for each set of N, output LaTeX source code laying out the N images on a page. Finally, pipe that into pdflatex and you'll have a nice PDF document with all your images embedded. It should be less than 10 lines of code, plus a little boilerplate.

That would paste the images at their original resolution and rely on the PDF renderer to size them to the paper. If some of the source images are very large, it could be a bit wasteful. I suppose you could use IM or equivalent to size the larger ones down a bit first.

Edit: I made a tiny example. My latex is awful, I'm sure you could write something much better:


puts <<HEADER

ARGV.each_slice(4).to_a.each do |images|
    puts "\\begin{center}"
    puts "\\thispagestyle{empty}"
    images.each {|filename| puts "\\includegraphics[width=2in]{#{filename}}"}
    puts "\\end{center}"
    puts "\\newpage"

puts <<FOOTER

Run with something like:

./layout.rb x/* > x.tex

To scan the directory x/ for jpg files and generate a latex file with images set out four up, then:

pdflatex x.tex
evice x.pdf

To generate the PDF and view it.