Files
brickr/README.Rmd
T
2018-05-06 10:35:07 -04:00

98 lines
3.8 KiB
Plaintext

---
title: "LEGO Mosaics in R"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
source("0_Functions.R")
```
## Introduction
The functions in the file `0_functions.R` convert a jpg or png image into a mosaic of available LEGO colors and bricks using the R [tidyverse](https://www.tidyverse.org/) and the `jpeg` or `png` packages.
A full explanation can be found on [this blog post](http://www.ryantimpe.com/2018/04/23/lego-mosaic1/) and this [follow-up post](http://www.ryantimpe.com/2018/05/07/lego-mosaic2/).
```{r m1_orig, fig.width = 3, fig.height=3, echo = FALSE, message = FALSE, warning = FALSE}
mosaic1_orig <- readRDS("README_cache/m1_orig.RDS")
ggplot(mosaic1_orig$Img_scaled, aes(x=x, y=y, fill = color)) +
geom_raster()+
scale_fill_identity() +
coord_fixed(expand = FALSE) +
theme_minimal() +
theme_lego +
theme(axis.line = element_blank(),
axis.ticks = element_blank())
```
```{r m1_set, fig.width = 3, fig.height=3, echo = FALSE, message = FALSE, warning = FALSE}
mosaic1 <- readRDS("README_cache/m1_lego.RDS")
mosaic1 %>% display_set()
```
This process is competed in a few distinct steps:
- `scale_image()` reduces the image to a number of brick "pixels". Providing a single value, such as `48`, crops the image to a square. Inputting a 2-element array, `c(56, 48)`, will output a rectangular image of `c(width, height)`.
- `legoize()` converts every brick-sized pixel in the scaled image to an official LEGO brick color. Those colors are stored in `Colors/Lego_Colors.csv`. By default, the functions look at only currently produced, non-transparent colors.
- `collect_bricks()` looks for adjacent groups of the same color to replace single 1 x 1 bricks with larger bricks.
- `display_set()` renders the LEGO mosaic as a plot for viewing, creating the image above.
```{r m1_demo, echo=TRUE, eval=FALSE}
mosaic1 <- readJPEG("Images/goldengirls.jpg") %>%
scale_image(48) %>%
legoize() %>%
collect_bricks()
mosaic1 %>% display_set()
```
## LEGO Mosaics IRL
Additional functions assist in the translation from the LEGO mosaic image into a real LEGO set.
### Instructions
Use `generate_instructions()` to break the LEGO mosaic image into easier-to-read steps for building the set. This defaults to 6 steps, but passing any integer value will generate that many steps.
```{r m1_instructions, fig.width = 8, fig.height=7, message = FALSE, warning = FALSE}
mosaic1 %>% generate_instructions(9)
```
### Piece list and count
Use `display_pieces()` to generate a graphic and count of all required plates or bricks (for stacked mosaics). These are sorted by color and size for easy purchase on LEGO.com's [Pick-a-Brick](https://shop.lego.com/en-US/Pick-a-Brick) section using the advanced search option. Alternatively, use `table_pieces()` to produce a data frame table of all required bricks.
```{r m1_pieces, fig.width = 9, fig.height=9, message = FALSE, warning = FALSE}
mosaic1 %>% display_pieces()
```
## Stacked mosaics
The default produces instructions for a flat LEGO mosaic, with all bricks placed "stud-up" on a plate. Alternatively, specifying `mosaic_type = "stacked"` in the `collect_bricks()` function will generate a mosaic where all bricks are stack on top of each other, resulting in the mosaic image being visible from the side.
A 1 x 1 LEGO brick is taller than it is wide by a ratio of 6/5, so it's recommended to use a wider image.
```{r m2_demo, eval=FALSE}
m2_lego <- readJPEG("Images/goldengirls2.jpg") %>%
scale_image(c(56, 48)) %>% #c(Width, Height) for rectangle
legoize() %>%
collect_bricks("stacked")
```
```{r m2_set, fig.width = 4, fig.height=4, echo = FALSE, message = FALSE, warning = FALSE}
mosaic2 <- readRDS("README_cache/m2_lego.RDS")
mosaic2 %>% display_set()
```
```{r m2_pieces, fig.width = 9, fig.height=7, message = FALSE, warning = FALSE}
mosaic2 %>% display_pieces()
```