My previous post on building LEGO mosaics using R went viral with literally tens of people viewing the page, starring the GitHub repo1, or sharing their own creations on Twitter. David Smith from Microsoft’s Revolutions blog even wrote a post about it.
In response, I’ve received a few emails and suggestions for improvements, so I’ve updated the functions in the repository with some new features.
My original goal was to build mosaics that would fit on the standard 48-stud square base plate. Most of the user examples I have seen have used larger dimensions, so I’ve relaxed the square restriction in the
You can now pass an array of two values to this function, the first value specifying the brick-width of the desired mosaic and the second value specifying the height.
readJPEG("LEGOMosaic/goldengirlsW.jpg") %>% scale_image(c(72, 48)) %>% #Single value for square, c(W, H) for rectangle legoize() %>% collect_bricks() %>% display_set()
The default mosaics are designed to be built by placing a single layer of LEGO plates on a base plate, stud-side up. I received an email from someone who wanted a mosaic that could be built upwards, so the side of the bricks create the image.
This was actually a pretty simple fix. Using
%>% collect_bricks("stacked") narrows the brick search to just 2x1, 2x2, 2x3, and 2x4 bricks placed horizontally and staggers them - just like laying actual bricks. The charting functions then render the images with a resolution reflecting the proportions of the side of a brick.2
LEGOmosaic <- readJPEG("LEGOMosaic/goldengirls2.jpg") %>% scale_image(c(72, 48)) %>% legoize() %>% collect_bricks("stacked") LEGOmosaic %>% display_set()
I’ve added two new functions to make it easier figure out which LEGO bricks or plates you will need to build the mosaics. The function
table_pieces() produces a tibble of required pieces for easy printing.
LEGOmosaic %>% table_pieces() %>% head() %>% knitr::kable()
|LEGO Brick Color||1 x 2||2 x 2||3 x 2||4 x 2|
A more fun, graphical alternative is to use
display_pieces(). This image renders slightly differently between stacked and flat mosaics.
LEGOmosaic %>% display_pieces()
On the LEGO Pick-a-Brick page, you can use the Advanced Search section to filter by each color and select the number of pieces you need.
The boring parts
I’ve made a few behind-the-scenes edits for those who use intermediate function output. Each function now returns a list object rather than a data frame. This allows the functions to pass metadata (like the stacked vs flat specification) to other functions.
LEGOmosaic %>% summary() %>% knitr::kable()
generate_instructions() function now returns a graphic rather than a data frame, no longer requiring the user to plot them manually.
These updates to the functions should make the process of creating a LEGO mosaics a bit easier and more accessible. Next time around, I’ll build a package.
Try it out! Full script can be found on GitHub!