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!
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!
- 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".
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:
- Image Preprocessing: quantize, downsample and export image
- Data Preprocess: color to key mapping, run-length encoding, export problem vectors
- Optimization: solve multiple-knappsack problem
- Postproces: decode run-length to image reconstruct
- Image reconstruction: reconstruct image with block highlights
- Bill of Materials: Get the BOM and add it to the image
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