<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Blake Bullwinkel on Medium]]></title>
        <description><![CDATA[Stories by Blake Bullwinkel on Medium]]></description>
        <link>https://medium.com/@blakebullwinkel?source=rss-2c1842aeade9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*E5SI4gk-3RM5bY10opeVUw.jpeg</url>
            <title>Stories by Blake Bullwinkel on Medium</title>
            <link>https://medium.com/@blakebullwinkel?source=rss-2c1842aeade9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 08:33:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@blakebullwinkel/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using Python to make a LEGO mosaic]]></title>
            <link>https://medium.com/@blakebullwinkel/i-used-python-to-make-a-lego-mosaic-for-valentines-day-d5b6c274e08f?source=rss-2c1842aeade9------2</link>
            <guid isPermaLink="false">https://medium.com/p/d5b6c274e08f</guid>
            <category><![CDATA[creativity]]></category>
            <category><![CDATA[art]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[lego]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Blake Bullwinkel]]></dc:creator>
            <pubDate>Thu, 09 Feb 2023 22:19:21 GMT</pubDate>
            <atom:updated>2025-12-30T03:37:06.984Z</atom:updated>
            <content:encoded><![CDATA[<h4>And how coding skills can unlock the full potential of your creativity</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EcH-PU2jApzq1fmZt6luDQ.png" /></figure><p>Why learn to code? I think the answer is simple: knowing how to code empowers you to build cool things that you wouldn’t be able to build — or at least, would <em>really</em> struggle to build — without a computer. And not just things in the digital world, but real stuff, too. In this article, I’ll show you how to use some basic Python programming to make a personalized mosaic out of LEGO.</p><p>It turns out that LEGO has an <a href="https://www.lego.com/en-us/mosaic-maker">online tool</a> that can create custom mosaics and sell you the bricks to build them, but it’s limited to greyscale and yellow pieces. I uploaded a couple photos and didn’t love how they turned out, so I figured I’d try coding my own mosaic maker that has access to more colors (and is hopefully overall less expensive than LEGO’s $130 mosaic maker set).</p><p>Let’s start by defining the problem more clearly. We’d like to write a program that takes in an image and outputs: 1) the pixelated LEGO mosaic, and 2) the LEGO pieces required to build that mosaic. LEGO sells a <a href="https://www.lego.com/en-us/product/gray-baseplate-11024">48x48 grey baseplate</a>, so let’s make this our canvas. We can use LEGO’s <a href="https://www.lego.com/en-us/pick-and-build/pick-a-brick?gclid=Cj0KCQiAofieBhDXARIsAHTTldoZeI056h5sFqgJObHj6775YKNu5j-NknyNrO4dZAwZHfi_TPZ62GUaAgkzEALw_wcB&amp;ef_id=Cj0KCQiAofieBhDXARIsAHTTldoZeI056h5sFqgJObHj6775YKNu5j-NknyNrO4dZAwZHfi_TPZ62GUaAgkzEALw_wcB:G:s&amp;s_kwcid=AL!790!3!330149568438!e!!g!!lego%20shop%20pick%20a%20brick!1699277907!69151109034&amp;Buffer=&amp;cmp=KAC-INI-GOOGUS-GO-US-EN-RE-PS-BUY-CREATE-BRAND-SHOP-BC-EX-RN-PICK_A_BRICK">Pick a Brick</a> to select the individual pieces that will fill the canvas.</p><p>To make the final product look a little more polished, let’s add a frame around the outer edge of the baseplate using black tiles. This leaves us with 46x46 pixels for the actual mosaic.</p><p>Okay, enough setup. Let’s get started.</p><h4>Step 1: Choose a picture</h4><p>This step might sound too simple to warrant its own section, but I’ve included it to highlight a couple constraints. We have only 46x46 pixels to work with, so the picture we choose: 1) must be square, and 2) should probably be a close-up, especially if it includes faces. I know it’s out of date, but this cropped photo of Pete Davidson and Kim Kardashian satisfies these criteria, so let’s use it for this demo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F7OCBr4oZP7zZZY-I0Iwyg.jpeg" /><figcaption>Source: Getty Images</figcaption></figure><h4>Step 2: Pixelate the image to 46x46 squares</h4><p>Now that we have a picture, let’s pixelate it to 46x46 solid color squares. In the cell below, we do this in just a couple lines of code using Pillow (the Python Imaging Library, or PIL). It doesn’t matter too much, but if you’re curious, we’re telling PIL to transform the full-size image to 46x46 pixels using something called <a href="https://en.wikipedia.org/wiki/Bilinear_interpolation">bilinear resampling</a>. 46x46 is the correct resolution for our mosaic, but it’s really small, so let’s make the individual pixels larger by scaling the image back up to its original size.</p><pre>from PIL import Image<br>import numpy as np<br><br># load the image<br>file_path = &quot;./pete_kim.jpg&quot;<br>img = Image.open(file_path)<br><br># resize image down to 46 x 46 pixels<br>small_img = img.resize((46, 46), resample=Image.Resampling.BILINEAR)<br><br># scale back up to original size<br>pixelated_img = small_img.resize(img.size, Image.Resampling.NEAREST)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a9Pyhtw7cpmF4HOJ_nJRpA.png" /></figure><h4>Step 3: Convert the 46x46 pixel image to a numpy array</h4><p>For reasons that will become clear soon, let’s convert our 46x46 pixel image into a numpy array, which will give us the (R, G, B) values of each pixel. After running the two lines of code below, you should find that the array has shape (46, 46, 3), where the “3” refers to the R, G, B color channels.</p><pre># convert image to numpy array<br>pixelated_img_arr = np.array(small_img)<br>print(pixelated_img_arr.shape)</pre><p>Note that if you get a shape of (46, 46, 4), this probably means that your input is a PNG, which has an extra “alpha” channel for transparency. To make things easier, I would recommend converting to JPG.</p><h4>Step 4: Decide which LEGO bricks to use for the mosaic</h4><p>You can probably guess where this is going — now that we have a pixelated version of our image, we can map each pixel to a 1x1 LEGO brick. But first, we have to decide which bricks to use. This is entirely up to you, but I decided to search Pick a Brick for flat, round tiles because they’re only $0.03 each. I also filtered for tiles in the “Bestseller” category, which can be shipped to US addresses within five business days. This leaves us with the following eight tiles to choose from.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q_PdbrKCueI3L42bI8G0rQ.png" /></figure><h4>Step 5: Map pixel colors to LEGO bricks</h4><p>Now for the fun part! To map pixels to LEGO tiles, we need to define a distance function that tells us how different any pixel is from any LEGO tile. Then, for each pixel in the image, we can simply calculate the distance between that pixel and all eight LEGO tiles and choose the tile corresponding to the smallest distance.</p><p>Given that we are interested in how similar the pixels and LEGO tiles are in terms of color, we should use some distance function over their (R, G, B) values. One popular choice is the <a href="https://en.wikipedia.org/wiki/Euclidean_distance">Euclidian distance</a>, which is defined for a pair of (R, G, B) tuples in the code cell below.</p><pre># calculate Euclidian distance between two RGB tuples<br>def get_rgb_distance(rgb1, rgb2):<br>    r1, g1, b1 = rgb1<br>    r2, g2, b2 = rgb2<br>    d = np.sqrt(np.sum([(r2-r1)**2, (g2-g1)**2, (b2-b1)**2]))<br>    return d<br><br># dictionary with LEGO tile colors and RGB values<br>lego_colors = {<br>    &quot;aqua&quot;: (34,163,159),<br>    &quot;black&quot;: (47,47,47), <br>    &quot;brown&quot;: (130,94,65), <br>    &quot;green&quot;: (96,186,70),<br>    &quot;grey&quot;: (124,124,124), <br>    &quot;peach&quot;: (247,188,153),  <br>    &quot;sand&quot;: (184,167,115),<br>    &quot;violet&quot;: (136,56,116)<br>}<br><br># get the LEGO pixel with smallest Euclidian distance from pixel<br>def get_closest_lego_pixel(pixel):<br>    closest_lego_pixel = None<br>    min_distance = np.inf<br>    for lego_pixel in lego_colors.values():<br>        d = get_rgb_distance(pixel, lego_pixel)<br>        if d &lt; min_distance:<br>            min_distance = d<br>            closest_lego_pixel = lego_pixel<br>    return closest_lego_pixel</pre><p>We also need a dictionary mapping the eight LEGO tiles to their RGB values, which I determined using a <a href="https://imagecolorpicker.com/en">free color picker</a>. Finally, we have also defined a function that returns the (R, G, B) tuple of the LEGO tile that minimizes the Euclidian distance to a given pixel.</p><h4>Step 6: Create the LEGO mosaic!</h4><p>To recap, we can now map any pixel to the LEGO tile that is most similar in color (as measured by Euclidian distance). Now all that’s left to do is apply this procedure to each of the 46x46 pixels in our image. The function below does exactly that — let’s apply it to our pixelated image array.</p><pre># convert an array of pixels to an array of LEGO tiles<br>def get_lego_array(pixel_arr):<br>    pixel_arr_shape = pixel_arr.shape<br>    lego_array = np.zeros(pixel_arr_shape)<br>    w, h = pixel_arr_shape[0], pixel_arr_shape[1]<br>    for i in range(w):<br>        for j in range(h):<br>            pixel = pixel_arr[i][j]<br>            closest_lego_pixel = get_closest_lego_pixel(pixel)<br>            lego_array[i][j] = closest_lego_pixel<br>    lego_array = np.array(lego_array)<br>    return lego_array<br><br># apply the function to the image array<br>lego_array = get_lego_array(pixelated_img_arr)</pre><p>To view the result, we have to convert the numpy array back to a PIL Image object and scale it up to the original size of the image. Let’s see what we get.</p><pre># convert to PIL image<br>lego_img = Image.fromarray(lego_array.astype(&quot;uint8&quot;), &quot;RGB&quot;)<br><br># scale the lego image up<br>lego_img_rescaled = lego_img.resize(img.size, Image.Resampling.NEAREST)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VkhTzUQLKArana0AzdzFZQ.png" /></figure><p>Not bad! It’s obviously hard to make out precise features, but I think I’d recognize Pete and Kim in these 46x46 pixels even if I hadn’t seen the original photo. And much like a pointillist painting, their faces become more recognizable if you squint or view the image from farther away. I’m no artist, but I’d say the colors work pretty well together.</p><p>Just one thing is bothering me — our code has picked up on the white text in the background of the source photo and mapped it to four grey squares. Let’s manually swap these out for aqua pixels so that we get a nice, smooth background.</p><pre># specify the locations of the grey pixels to turn aqua<br>grey_pixels = [(5,24), (6,24), (7,25), (8,25)]<br><br># change grey pixels to aqua<br>for pixel in grey_pixels:<br>    i, j = pixel<br>    lego_array[i][j] = lego_colors[&quot;aqua&quot;]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*otycznD9RmsUDrRPT-RNwg.png" /></figure><h4>Step 7: Count the number of LEGO bricks to buy</h4><p>Assuming you’re committed to actually building our beautiful LEGO mosaic, you’ll need to know how many of each brick to buy. By inverting the dictionary of LEGO colors we used earlier, we can easily count the number of each tile we need by looping through the array of LEGO pixels.</p><pre># invert the LEGO colors dictionary<br>inv_lego_colors = {v: k for k, v in lego_colors.items()}<br><br># get dictionary of colors counts in the LEGO numpy array<br>def get_color_counts(lego_array):<br>    color_counts = {k: 0 for k in lego_colors.keys()}<br>    lego_array_shape = lego_array.shape<br>    w, h = lego_array_shape[0], lego_array_shape[1]<br>    for i in range(w):<br>        for j in range(h):<br>            lego_pixel = lego_array[i][j]<br>            lego_color = inv_lego_colors[tuple(lego_pixel)]<br>            color_counts[lego_color] += 1<br>    return color_counts<br><br># get the counts and print the result<br>color_counts = get_color_counts(lego_array)<br>print(color_counts)</pre><p>Printing out the result, here’s what we get.</p><pre>{&#39;aqua&#39;: 371,<br> &#39;black&#39;: 843,<br> &#39;brown&#39;: 177,<br> &#39;green&#39;: 0,<br> &#39;grey&#39;: 140,<br> &#39;peach&#39;: 312,<br> &#39;sand&#39;: 272,<br> &#39;violet&#39;: 1}</pre><p>As we would hope, these counts add up to 46x46 = 2116 pieces. For the black frame, I decided to buy 60 1x3 tiles and four 1x2 tiles, which fit neatly around the edge of the 48x48 baseplate.</p><p>And there you have it. With some basic Python programming and a little bit of creativity, we have figured out how to build something cool that we wouldn’t have been able to build ourselves. When used the right way, a computer can be a powerful extension of your brain to help you unlock the full potential of your creativity. So have fun and play around with the code here to come up with your own, unique creations. I look forward to seeing what you build.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d5b6c274e08f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[An introduction to the magic of machine learning]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science/an-introduction-to-the-magic-of-machine-learning-6b213e88b54c?source=rss-2c1842aeade9------2"><img src="https://cdn-images-1.medium.com/max/2600/1*3XV1EEmmzxz536Y5vHZsig.png" width="2731"></a></p><p class="medium-feed-snippet">With math instead of code</p><p class="medium-feed-link"><a href="https://medium.com/data-science/an-introduction-to-the-magic-of-machine-learning-6b213e88b54c?source=rss-2c1842aeade9------2">Continue reading on TDS Archive »</a></p></div>]]></description>
            <link>https://medium.com/data-science/an-introduction-to-the-magic-of-machine-learning-6b213e88b54c?source=rss-2c1842aeade9------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b213e88b54c</guid>
            <category><![CDATA[education]]></category>
            <category><![CDATA[science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Blake Bullwinkel]]></dc:creator>
            <pubDate>Thu, 23 Dec 2021 16:22:08 GMT</pubDate>
            <atom:updated>2025-03-07T23:01:20.858Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[How math and science move in opposite directions]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@blakebullwinkel/how-math-and-science-move-in-opposite-directions-53820a836c4c?source=rss-2c1842aeade9------2"><img src="https://cdn-images-1.medium.com/max/2500/1*LbdFmUexx-jdP2Af8iRTRA.png" width="2500"></a></p><p class="medium-feed-snippet">And how they might ultimately be one and the same&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@blakebullwinkel/how-math-and-science-move-in-opposite-directions-53820a836c4c?source=rss-2c1842aeade9------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@blakebullwinkel/how-math-and-science-move-in-opposite-directions-53820a836c4c?source=rss-2c1842aeade9------2</link>
            <guid isPermaLink="false">https://medium.com/p/53820a836c4c</guid>
            <category><![CDATA[nature]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[science]]></category>
            <category><![CDATA[philosophy]]></category>
            <category><![CDATA[physics]]></category>
            <dc:creator><![CDATA[Blake Bullwinkel]]></dc:creator>
            <pubDate>Thu, 28 Jun 2018 08:27:46 GMT</pubDate>
            <atom:updated>2018-06-28T08:27:46.672Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[What’s the tallest loop-the-loop roller coaster that we could ever build and ride safely?]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/rock-bottom/what-s-the-tallest-loop-the-loop-roller-coaster-that-we-could-ever-build-and-ride-safely-cfa173bada0c?source=rss-2c1842aeade9------2"><img src="https://cdn-images-1.medium.com/max/1176/1*YULGji11aUwrcu11LGaqDw.png" width="1176"></a></p><p class="medium-feed-snippet">Amusement park physics taken to the next level.</p><p class="medium-feed-link"><a href="https://medium.com/rock-bottom/what-s-the-tallest-loop-the-loop-roller-coaster-that-we-could-ever-build-and-ride-safely-cfa173bada0c?source=rss-2c1842aeade9------2">Continue reading on Rock Bottom »</a></p></div>]]></description>
            <link>https://medium.com/rock-bottom/what-s-the-tallest-loop-the-loop-roller-coaster-that-we-could-ever-build-and-ride-safely-cfa173bada0c?source=rss-2c1842aeade9------2</link>
            <guid isPermaLink="false">https://medium.com/p/cfa173bada0c</guid>
            <category><![CDATA[physics]]></category>
            <category><![CDATA[science]]></category>
            <category><![CDATA[mathematics]]></category>
            <dc:creator><![CDATA[Blake Bullwinkel]]></dc:creator>
            <pubDate>Fri, 26 Feb 2016 16:57:05 GMT</pubDate>
            <atom:updated>2016-02-26T17:25:29.059Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[A Historic Day for the World of Competitive Rubik’s Cube Solving]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@blakebullwinkel/a-historic-day-for-the-world-of-competitive-rubik-s-cube-solving-33a92a5f3d1c?source=rss-2c1842aeade9------2"><img src="https://cdn-images-1.medium.com/max/618/1*oUPLqVKyVWFsfZSwsGBooA.png" width="618"></a></p><p class="medium-feed-link"><a href="https://medium.com/@blakebullwinkel/a-historic-day-for-the-world-of-competitive-rubik-s-cube-solving-33a92a5f3d1c?source=rss-2c1842aeade9------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@blakebullwinkel/a-historic-day-for-the-world-of-competitive-rubik-s-cube-solving-33a92a5f3d1c?source=rss-2c1842aeade9------2</link>
            <guid isPermaLink="false">https://medium.com/p/33a92a5f3d1c</guid>
            <category><![CDATA[rubiks-cube]]></category>
            <category><![CDATA[speedcubing]]></category>
            <category><![CDATA[world-record]]></category>
            <dc:creator><![CDATA[Blake Bullwinkel]]></dc:creator>
            <pubDate>Wed, 25 Nov 2015 14:15:17 GMT</pubDate>
            <atom:updated>2015-11-25T22:48:31.870Z</atom:updated>
        </item>
    </channel>
</rss>