How to fit multiple images in a page optimally using ImageMagick

1.6k views Asked by At

Suppose I have a bunch of images in a folder with different sizes. The goal is to fit images in a number of pages (e.g. A4) in a way that the whitespace is minimal. There shouldn't be any compression or resizing involved. It is acceptable that some images would be rotated.
Here is what I came up with but doesn't try to "fit" images in any way:

montage *.jpg -mode concatenate -tile 2x2 -page A4 -geometry +20+20 out.pdf

Is it possible using imagemagick and montage switches?
I guess the computational geometry algorithm should change the 2x2 and +20+20 part of the command above, right?

1

There are 1 answers

5
fmw42 On

In ImageMagick 7 (7.0.10-23 or higher), there is a new feature for doing collages called Ashlar. But it resizes to fit all the provided images into the space allocated.

See https://imagemagick.org/script/formats.php#pseudo

magick *.jpg -define ashlar:best-fit=true ashlar:canvas.png[1000x1000+5+5]

enter image description here

The [1000x1000+5+5] specifies the output dimensions and the minimum spacing in x and y between images.


Here is A4 size result:

magick *.jpg -define ashlar:best-fit=true ashlar:canvas2.jpg[595x842++0+0]

enter image description here