1

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?

Zeta.Investigator
  • 911
  • 2
  • 14
  • 31

1 Answers1

5

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

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Thanks! So `montage *.jpg -define ashlar:best-fit=true -mode concatenate -tile 2x2 -page A4 -geometry +5+5 out.pdf` to best fit in a number A4 pages? – Zeta.Investigator Oct 19 '20 at 19:16
  • 1
    As far as I know, it does not work in montage. It only works in IM 7 using magick .... ashlar:result.jpg and it resizes the images to fit your specified output size. – fmw42 Oct 19 '20 at 19:21
  • Thanks. So it resizes to fit... I wanted to fit a collection of images on m pages without changing their size. – Zeta.Investigator Oct 19 '20 at 19:25
  • There is no method for that. You will have to script something. Best you can do is compute how may image you can put on each page, then use montage, then combine all the montages into one PDF of multiple pages. – fmw42 Oct 19 '20 at 21:31
  • I believe that if you have few enough images for a given size output, then Ashlar will not resize. But you would have to make some computation for that. – fmw42 Oct 19 '20 at 21:32