Packing rectangular image data into a square texture

TexturePacker picture TexturePacker · Nov 5, 2008 · Viewed 17.2k times · Source

I have N items of 2D image data that will be rectangular and I want to pack them into a single power of 2 texture as efficiently as possible.

A simple non-efficient and naive implementation of an algorithm to pack these rects would be easy to whip up, but I'm sure people have come up with algorithms to do this as space efficiently as possible. I've found various references to lightmap packing which is similar to what I'm looking for, but the algorithms for lightmapping tend to take non-rectangular images into account which actually complicates things more than I need them to be.

Does anyone have hints? Names of algorithms or paper authors I should google?

Thanks.

Answer

anon picture anon · Nov 5, 2008

I've needed to do this very thing you describe.

This is the Python code I used, it's a recipe in the Python Cookbook:

Recipe 442299: pack multiple images of different sizes into one image