This is a fork of the official RasterFairy that has Python 3 support. Since the official repo isn't maintained (there is an unmerged pull request for Python 3 from April, 2017), I made a Python 3 compatible version on my own.
Below is the README from the official RasterFairy repo.
The purpose of Raster Fairy is to transform any kind of 2D point cloud into a regular raster whilst trying to preserve the neighborhood relations that were present in the original cloud. A typical use case is if you have a similarity clustering of images and want to show the images in a regular table structure.
From the root directory, run:
pip install .
import rasterfairy #xy should be a numpy array with a shape (number of points,2) grid_xy = rasterfairy.transformPointCloud2D(xy) #grid_xy will contain the points in the same order but aligned to a grid
As I learned after publishing there is a very similar earlier developed technique called IsoMatch by O. Fried, S. DiVerdi, M. Halber, E. Sizikova and A. Finkelstein. Unfortunately I was not aware of it during my research and their solution works differently, but you might want to check it out and see if it's better suited to your requirements: IsoMatch Codebase
Another related technique is Kernelized Sorting by Novi Quadrianto, Le Song, Alex J. Smola. from 2009 Kernelized Sorting
Kyle McDonald's CloudToGrid project is a Python-based implementation of the Hungarian method.