LEGO mosaics: Two weeks later

Introduction

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.

Rectangular mosaics

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 scale_image() function.

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()

Stacked mosaics

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()

Piece list

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
Aqua 28 5 0 0
Black 4 0 0 0
Brick yellow 104 14 5 3
Bright blue 28 17 23 16
Bright orange 5 2 0 0
Bright purple 30 22 25 32

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()
Length Class Mode
Img_scaled 6 tbl_df list
Img_lego 8 tbl_df list
Img_bricks 8 tbl_df list
mosaic_type 1 -none- character
pieces 4 tbl_df list

The generate_instructions() function now returns a graphic rather than a data frame, no longer requiring the user to plot them manually.

Conclusion

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!


  1. Now with a proper README.md file! 🆗

  2. A 1-unit LEGO brick is not a square, but is taller than it is wide by a ratio of 6/5.

Related