Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a montage with ImageMagick from images with different size and aspect ratio?

I have a lot of images, different sizes and aspect ratios. Is it possible to make a montage of them? I mean to optimally arrange them in rows after I set a common height for the images which build up a common row. Images aspect ratios aren't allowed to modify of course, and none of the images are allowed to omit from the final montage nor duplication.

The height of picture rows in the montage usually aren't equal, but their values should be kept in a minimal range (in statistical sense) or in other words: standard deviation from the mean value of the row heights must be minimized.

Desired width and height of the montage are given (a.)

Or the width and an allowed ratio range (or equivalently height range) is given (b.), for example width must be 1024 pixel, height must be so that w/h < 0.9 and w/h > 0.8

1.) Images must be packed in the montage in their initial fixed order. In this case one must find the images after what a new image row should be started in the montage (easy).

2.) Order of images is allowed to be altered. In this case one must find a permutation which lead to a minimalization of the standard deviation of the final row heights when each image is packed into the montage (hard).

For example:

enter image description here

enter image description here

like image 907
Konstantin Avatar asked Feb 12 '14 09:02

Konstantin


People also ask

How do I resize an image in Imagemagick?

To resize an image to specific dimensions, use the convert command with an input file, the -resize parameter, your preferred dimensions, and an output filename: convert original. png -resize 100x100 new.

How do I add aspect ratio to an image?

To create an exact aspect ratio, divide the height by the width. For example: 2:3 aspect ratio: 3 ÷ 2 = 1.5, so you'd drag the slider to 150. 3:2 aspect ratio: 2 ÷ 3 = .

What is montage geometric?

The montage command has an option called "-geometry" which helps you to set the thumbnail size and the space between each image. The default -geometry setting is '120x120>+4+3'. Meaning - It will produce 120×120 thumbnails with 4 pixels on the left and right of each image and 3 pixels below.


1 Answers

I'm not sure I understand your question correctly.

Here is what I make of it. Assuming you have 8 different images of different sizes. For demo purposes, I'll let ImageMagick create these as 8 different color patches:

convert -size  90x90  xc:yellow  y.png
convert -size 120x120 xc:red     r.png
convert -size  60x210 xc:green   g.png
convert -size 150x180 xc:blue    b.png
convert -size  30x60  xc:cyan    c.png
convert -size 150x90  xc:magenta m.png
convert -size  90x120 xc:gray    Gr.png
convert -size 120x90  xc:black   K.png

You can montage these patches in many different ways:

convert \( y.png r.png g.png  b.png +append \)  \
        \( c.png m.png Gr.png K.png +append \)  \
       -append                                  \
       -mattecolor lightblue                    \
       -frame 1x1                               \
        montage0.png

This command does not scale the different patches. It places them in 2 rows a 4 patches and montages them in their original sizes. The white spaces is where the patches do not "fit":

montage0.png

convert \( y.png r.png g.png  b.png -resize x60 +append \)  \
        \( c.png m.png Gr.png K.png -resize x60 +append \)  \
       -append                                              \
       -mattecolor lightblue                                \
       -frame 1x1                                           \
        montage1.png

This command scales all different patches to a common height of 60 pixels (preserving their respective aspect ratios) and places them in 2 rows a 4 patches:

montage1.png

convert \( y.png r.png g.png  b.png -resize 60x +append \)  \
        \( c.png m.png Gr.png K.png -resize 60x +append \)  \
       -append                                              \
       -mattecolor lightblue                                \
       -frame 1x1                                           \
        montage2.png

This command scales all different patches to a common width of 60 pixels (preserving their respective aspect ratios) and places them in 2 rows a 4 patches:

montage2.png

convert \( y.png r.png g.png  b.png -resize 60x80\! +append \)  \
        \( c.png m.png Gr.png K.png -resize 60x80\! +append \)  \
       -append                                                  \
       -mattecolor lightblue                                    \
       -frame 1x1                                               \
        montage3.png

This command scales all different patches to dimensions of 60x80 pixels (overriding their original aspect ratios) and places them in 2 rows a 4 patches:

montage3.png

like image 53
Kurt Pfeifle Avatar answered Nov 07 '22 04:11

Kurt Pfeifle