We’ve extended Inspire Early Bird Pricing until March 1. Register now and enjoy 20% off conference passes and 10% off training passes. P.S. Don’t forget to bring friends! When you sign up for five or more tickets, you get an extra 20% discount on conference passes. Learn more now.

Data Science

Machine learning & data science for beginners and experts alike.
ACE Emeritus
ACE Emeritus



Legolytics Part 1: Extracting Colours From an Image With Alteryx and R (you are here)

Legolytics Part 2: Assigning the Lego Colour Palette

Legolytics Part 3: All Possible Bricks

Legolytics Part 4: Optimizing Cost

Legolytics Part 5: The Product

Setting the Scene

Around 18 months ago I visited the Lego store with my dad, brother, and nephew (using my nephew as an excuse to act like children). On the visit we came across something that we thought was an ingenious idea, the Lego Mosaic Maker.

It’s essentially a photo booth, but instead of printing out a tiny passport-sized photo of your face, it gives you your own personal Lego set, allowing you to construct a self-portrait with 1x1 lego bricks.


Fantastic idea, right? I thought so; but what I didn’t think was a fantastic idea was the price, I thought it was a bit steep at £100, or $130 (mind, I am a bit tight, so some might think that’s quite well priced for such a personalised product).

The maths (at least in my case) looked something like…

£100 = cost of set

£178 = cost of sending just my dad to the store to get picture taken (Yes trains are expensive in the UK!)

So, it comes out at £278 for the experience: not good.

I then did some basic math based on the size of the image and the cost of Lego bricks.

The image is a 48x48 grid, that’s 2304 pixels.

Each 1x1 Lego piece costs £0.06, so that’s £138.04 to build the image.

Then you need a base plate, this is one of those big flat square things that you stick your Lego to, this is £13. So, at the very worst, if I can build a mechanism to do this myself, it will cost me £151.04. That’s a saving of 54% (imagine if you reported that kind of saving In a business environment, your bosses would love you!).

So, for the reason of math, and some Alteryx enjoyment, I thought what the heck, let’s try this, and this blog (or 5 blogs), will detail the key stepping stones which allowed me to build a Lego portrait with Alteryx.

Streaming Image Into Alteryx


The first task that we face is actually extracting, pixel by pixel, the different colours within the image. For the sake of this exercise, I will use my friend (and current US Alteryx Grand Prix Champion) @PhilipMannering's face.



Now, unfortunately, there is no native way to perform this task using Alteryx, but thankfully, Alteryx is a code-friendly platform, and thanks to the integration with R, this task becomes possible.

I can take very little credit for the development of the script, which is largely ripped from this post on RBloggers, but it did take some small adaptions for the task in-hand.


So, step 1: let’s load the libraries that we need to use for this task.






We can then use a command, which forms part of the jpeg library, to read our image as a dataframe (because Alteryx loves dataframes!). Here I create a table titled original.



original <- readJPEG("C:/Users/BenMoss/Desktop/Selfie.jpg")



Now, I don’t know about you, but my phone takes pictures in a slightly higher resolution than 48x48 (that’s a measly 0.0023 megapixels, whereas your phone is probably something like 12), so we also need to reduce the size of the image, which is where the function resize comes in, which forms part of the second library loaded, EBImage.


Here I am creating a second dataframe, titled Resized, which is our original image reduced to the pixel size needed, in this case, 48x48. 



resized <- resize(original, w = 48, h = 48)



We should then transform our object into a dataframe which contains a single row per pixel, with 3 columns, one for each of the three colours (red, green and blue), which forms RGB. Here I create a final object called df.



df = data.frame(
  red = matrix(resized[,,1], ncol=1),
  green = matrix(resized[,,2], ncol=1),
  blue = matrix(resized[,,3], ncol=1)



The final step is to stream this dataframe into Alteryx, so we can work with it as if it were a data table. This is done very simply with the write.Alteryx function. Here I write the data table to the output node labeled 1 on the R tool.



write.Alteryx(df, 1)





Thankfully, even in the images reduced image state, we can still tell that this is the Alteryx master himself, Phil Mannering.

Next Steps


Now we’ve brought our image into Alteryx, we must now perform the task of assigning actual Lego colours to the image, which involves a process of clustering, but that’s for part 2!