Results 1 to 6 of 6

Thread: Organizing images based on size.

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Organizing images based on size.

    My program utilizes various images of different sizes. I want to organize these images so that they all fit into a predetermined space without resizing any of them. They need to be organized in such a way to use as much of the predetermined space as possible.

    I guess it would be like organizing photos in one of those old photo albums (with the translucent, mildly adhesive, peel-back plastic sheet that held the photos in place) so that you could get as many photos on a page as possible.

    Or you could think of it sort of like Tetris? Except all the pieces are rectangles.

    How would I do this?

    I assume I would use a recursive function to loop through all the images to find out if they fit or not, then returning the best outcome?

    I tried searching for code samples but searching for Organizing Images or Organizing Shapes gives less than desirable results.

    Thanks.
    Prefix has no suffix, but suffix has a prefix.

  2. #2
    Addicted Member
    Join Date
    Dec 2009
    Location
    Arizona
    Posts
    185

    Re: Organizing images based on size.

    I would get the area of the rectangle you want to fit them into. Each time you add an image, subtract its area from the total area of the page. Start with the smallest images first, that compacts them the most, as best I know. That may not be the most optimum method for all I know, but that is how I would do it.
    .
    We must question the story logic of having an all-knowing all-powerful God, who creates faulty Humans, and then blames them for his own mistakes.
    GENE RODDENBERRY
    .
    http://www.tachufind.com
    .

  3. #3
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Organizing images based on size.

    Hi Troy,

    The term for what you are seeking is a 2D Nesting Algorithm. These are used by software for planning the most efficient use of sheet materials. I suspect that such software uses some rather sophisticated mathematics and logic.

    Meanwhile, skywola's approach might work if you intend to place the maximum number of images regardless of size. But if your aim is to fill the target area as efficiently as possible, I think it would be better to place the larger images first. The reason is that you need small images for filling up the leftover spaces between the larger ones, so it would be a mistake to use up all the small images first.

    In pseudocode terms, I would tackle the process as follows, starting with a generic collection of images.

    Code:
    1. Make a function for comparing two images, which returns True if the first image is larger in area than the second image
    
    2. Sort the collection of images using the function from step 1 as a comparer, largest first.
    
    3. Convert the collection to a Stack.
    
    4. Recursively:
    
        a. pop the first image from the stack.
     
        b. scan the whole target area from top-left to bottom-right for an empty rectangle large enough to contain the popped image.
    
        c. if no suitable rectangle is found and the stack is empty, exit.
    
        d. if no suitable rectangle is found and the stack is not empty, go back to step 4a.
    
        e. if a large enough free rectangle is found, place the popped image at top left in the rectangle.
    
        f. if the stack is not empty, go back to step 4a, else exit.
    This assumes you only want to fill one target area. If you intend to fill multiple areas as in the picture album analogy, you could transfer any unused images from steps 4c and 4d above to a new collection, and repeat the routine until there are no images left.

    Edit: Whether the above method produces the "best" result depends partly on the mix of image sizes. In some cases, selecting the smaller images first might yield a more efficient packing; or it might be best to start with medium-sized images. In other words, you could repeat the above routine using different sort criteria and see which produces the best result for a given mix of images.

    BB

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Organizing images based on size.

    As you may have gathered by now, what you want to do is not trivial. Search for "backpack problem" and you may find some interesting information about the problem and possible solutions.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Organizing images based on size.

    I just noticed an article in the CodeProject which offers "a fast algorithm to pack a series of rectangles of varying widths and heights into a single enclosing rectangle, with no overlap and in a way that minimizes the amount of wasted space in the enclosing rectangle". It appears to seek the smallest rectangle that will enclose a given set of rectangles. That's not quite the same as OP asks but I think it must be worth looking at.

    Here's the link: Fast Optimizing Rectangle Packing Algorithm for Building CSS Sprites

    BB

  6. #6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width