static BWImage clusterImages(L images) { if (empty(images)) null; int n = l(images); int w = (int) sqrt(n), h = (n+w-1)/w; int cw = 0, ch = 0; for (BWImage i : images) { cw = max(cw, i.getWidth()); ch = max(ch, i.getHeight()); } // TODO: check if there are too many full black rows/cols in images int iw = 1+w*(cw+1), ih = 1+h*(ch+1); // light gray background to distinguish between image and spacing BWImage img = new BWImage(iw, ih, 0.95f); for (int x = 0; x < iw; x += cw+1) for y to ih: img.setPixel(x, y, 0f); for (int y = 0; y < ih; y += ch+1) for x to iw: img.setPixel(x, y, 0f); for i to n: { int x = i % w, y = i / w; BWImage im = images.get(i); copyBWImage(im, 0, 0, img, 1+x*(cw+1)+(cw-im.getWidth())/2, 1+y*(ch+1)+(ch-im.getHeight())/2); } ret img; }