Skip to content

Given a pool of lego blocks and an image, this codeset solves the combinatorics to recreate the image in block-form.

License

Notifications You must be signed in to change notification settings

Chipdelmal/LegoOptimizer

Repository files navigation

Please have a look at my blog posts (post 1 & post 2) for more info on how this pipeline works, and the reasoning behind the solution!

Problem Statement

Given a pixel-based image and a pool of colored Lego blocks: can I recreate the image with my available blocks?

  • If so, how should I arrange them so that I don't run out of them without the image being completed?
  • If not, what blocks are missing for me to complete the image?

Unfortunately, this is not as easy of a problem as it looks at first glance and it will take image quantizing, run-length encoding, multiple-knapsack problems instances, and some image overlays to get it all done!

Features

  • Automatic image re-scaling.
  • Arbitrary color-palette quantization (or quantization to a given number of colors).
  • Prioritization of certain block-lengths provided by the user.
  • Arbitrary pools of blocks can be defined.
  • Highlighting of missing pieces for the image to be completed.
  • Generation of the "bill of materials".

Pipeline

To get the code up and running, have a look at the selections.py and constants files to setup the parameters of your portraits (might streamline it a bit in the future when I have some time).

Now, just run the main.sh file with:

main.sh FILE_PATH FILE_NAME

This bash wrapper runs the following scripts in order:

  1. Image Preprocessing: quantize, downsample and export image
  2. Data Preprocess: color to key mapping, run-length encoding, export problem vectors
  3. Optimization: solve multiple-knappsack problem
  4. Postproces: decode run-length to image reconstruct
  5. Image reconstruction: reconstruct image with block highlights
  6. Bill of Materials: Get the BOM and add it to the image

Dependencies

To run the code, install the following dependencies: matplotlib, opencv-python, Pillow, numpy, OR-Tools, compress-pickle, termcolor. Working in an independent virtual environment like anaconda and virtualenv is highly recommended!

pip install opencv-python
pip install ortools
pip install numpy
pip install matplotlib
pip install compress-pickle
pip install termcolor
pip install Pillow

Author

Héctor M. Sánchez C.

About

Given a pool of lego blocks and an image, this codeset solves the combinatorics to recreate the image in block-form.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published