[SOLVED] Combining images: How to optimize image placement?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
4aiman
Posts: 2
Joined: 2016-08-03T08:41:56-07:00
Authentication code: 1151

[SOLVED] Combining images: How to optimize image placement?

Post by 4aiman »

Hello Everyone!
Many thanks for the ImageMagick!


I'd like to know whether it is possible to combine multiple images of different sizes while optimizing the placement?

Say, there are some colored rectangles which represent images of different size
Now, I want to combine those into one image.

Here is what montage does:
Image

Considering white is a background, is it possible to optimize placement of the colored rectangles?
I.e. one can clearly see that there's enough space to move up both cyan and magenta rectangles.
Then the gray one might go all the way to the right and the black one either to the right or up.

The tricky part is that there might be several large rectangles which will combine with huge "spaces" in-between.
Is there any chance that ImageMagick will search such "holes" and "place" the rectangle currently being added to the image into one of those "holes" if there's enough "room" (i.e. some "hole" is bigger than the current rectangle)?
Last edited by 4aiman on 2016-08-03T22:56:41-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cmbining images: How to optimize image placement?

Post by fmw42 »

One thought is to order your images by size, largest first. Place the first image at the top left. Then search for the largest empty space and place the next largest image. Etc.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cmbining images: How to optimize image placement?

Post by snibgo »

A useful "Survey on two-dimensional packing": http://cgi.csc.liv.ac.uk/~epa/surveyhtml.html
snibgo's IM pages: im.snibgo.com
4aiman
Posts: 2
Joined: 2016-08-03T08:41:56-07:00
Authentication code: 1151

Re: Combining images: How to optimize image placement?

Post by 4aiman »

Thanks. snibgo!
The survey you've linked to is very useful :)
Marking this as "solved".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [SOLVED] Combining images: How to optimize image placement?

Post by fmw42 »

Did you implement one of those methods? If so, your code could be useful to others.
Post Reply