Lego Picture Generator

A Java pipeline that turns any photo into a buildable LEGO-plate mosaic — downscaling the image, matching real LEGO colors, packing plates with six different algorithms, and rendering a LEGO-look result

Original vs LEGO Output

Original

Original Jarvis photo before LEGO conversion

Greedy Pack Output

LEGO mosaic output using the greedy packing algorithm
Technology Stack
Java
SQLite (JDBC)
Rebrickable Data
Algorithms
Image Processing
Make

Project Overview

The Lego Picture Generator converts a PNG photo into a real, buildable LEGO mosaic. The pipeline downscales the image into a stud grid, matches each stud to the nearest real LEGO color from a Rebrickable SQLite database, then packs the grid into physical plates using six different packing algorithms — from a fast greedy heuristic to exact-cover search and simulated annealing. The output is a shopping list (bill of materials), an algorithm comparison report, and LEGO-look renders showing exactly how to build it.

Key Features

  • • Configurable downscaling that averages pixel blocks into a stud grid
  • • Nearest-color matching against real LEGO colors that actually exist as 1×1 plates
  • • Six plate-packing algorithms: greedy, ILP branch-and-bound, row RLE, per-component greedy, DLX (Algorithm X), and simulated annealing
  • • Packers only use part-and-color combinations that exist in the Rebrickable database, so every result is actually buildable
  • • Bill of materials generation with per-mode piece counts and a side-by-side comparison report
  • • Procedural LEGO-look rendering: flat stud mosaics and packed-plate images with visible knobs

Technical Approach

  • • Modeled plate packing as a set-partition problem: cover every stud exactly once with single-color plates while minimizing piece count
  • • Exact solvers (ILP and DLX) run branch-and-bound per same-color blob using 64-bit bitmasks, falling back to greedy for oversized regions
  • • Simulated annealing improves a greedy seed by re-packing local windows and accepting worse moves with a cooling temperature
  • • Connected-component decomposition isolates same-color regions so each blob can be solved independently
  • • SQLite queries filter the plate catalog to sizes and colors that physically exist, keyed by Rebrickable part data
  • • Grounded in published research — Knuth's Dancing Links, Land–Doig branch-and-bound, and Kirkpatrick's simulated annealing

Results & Insights

Algorithm Comparison

Running all six packers on the same stud grid produces a comparison report of piece counts, runtimes, and deltas versus the greedy baseline. Exact methods find provably optimal packings for small same-color regions, while greedy and annealing scale to full images — a practical demonstration of the tradeoff between solution quality and computation time.

Development Insights

This project combined image processing, database-backed constraint filtering, and combinatorial optimization in one pipeline. Implementing the same problem six different ways — and reading the research behind each approach — gave me a much deeper understanding of exact-cover formulations, branching heuristics, and metaheuristic search than any single implementation would have.

Future Additions

  • Website for public use — a web front end where anyone can upload a photo, tune the mosaic size, and get back renders and a shopping list without installing anything
  • 3D rendering — an interactive 3D view of the finished mosaic with real plate geometry, so the build can be inspected from any angle before buying pieces
6

Packing Algorithms

Exactly Once

Every Stud Covered (Set Partition)

100%

Buildable — Only Real Part + Color Combos