The Sock Club Magical Color Machine

At Sock Club we're obsessed with making the best socks. We often compare ourselves to craft breweries. We put the same dedication and focus into making a unique and differentiated product (socks in our case) that your favorite local brewery puts into brewing beer. The internet has made making textiles extremely competitive. So we're always looking for ways to make better socks. We have amazing designers at Sock Club who are constantly make new sock designs. You might be amazed at how many sock designs we go through before deciding on which one to send to our subscribers. Here's a screenshot of our Real Time Board which we use to make design choices.

Image of Realtimeboard

Each of those multicolor J looking things is a different design. And for each design we make multiple colorways. Deciding the colorways is the last step in finalizing the designs that get made. We were having trouble deciding on the colorways because we have a lot of constraints like how does this colorway expand and fit into our current offering of sock designs. Last September we were having trouble deciding on a colorway. As a mathematician and software developer I wanted to formalize the problem we were trying to solve. We have about 42 different colors of yarn always in stock and each design on average has 5 different colors. So, for an average design there are 42 choose 5 different colorways. That's 850668 different colorways to pick from to send to our mills in North Carolina for production. Our designers have a great intuition on what colors to choose. But I thought there must be systematic way to search over these colorways. Which gets us to the point of this blog post - The Sock Club Magical Color Machine.

So here's an example of a file we send to our mills for production. It's a pretty simple bitmap which our production facility converts into a file that our knitting machines reads to produce socks. Yay!

In [15]:
SOCK_MILL_FILE = 'https://www.sockclub.com/images/squiggles.bmp'

from io import BytesIO
from urllib.request import urlopen
from PIL import Image

file = BytesIO(urlopen(SOCK_MILL_FILE).read())
sock_bitmap = Image.open(file)
sock_bitmap
Out[15]:

Here's a video I took of one of our knitting machines (pretty cool I think):

In [3]:
from IPython.display import YouTubeVideo
YouTubeVideo('GR_k0Mwe4ko', width=800, height=500)
Out[3]:

I thought if I took this file and had a computer just randomly choose each color from our 42 stock yarn colors I could make a new colorway instantly. Here's how the Sock Club Magical Color Machine makes a new design:

In [31]:
import numpy as np
import random

# These are all the yarn colors we have in stock
YARN_COLORS = ["#ece7d9", "#cdbfaa", "#000000", "#f4f3f3", "#f5bfca", "#1963ac", "#4f5457", "#f68d3f", "#9aa9ab", "#24aae1", "#db1280", "#d8cd4a", "#774679", "#288741", "#0d4166", "#92c9d5", "#9fc73c", "#6b232a",
          "#aa8e74", "#052a3f", "#2e3032", "#14b1be", "#ee502a", "#1e3e22", "#652257", "#d92026", "#b72327", "#129e7a", "#3f2c25", "#452971", "#f8bb15", "#00867b", "#6c9d40", "#29358d", "#f8e03a", "#5ca2d8", "#e34f94"]


sock_bitmap = sock_bitmap.convert('RGB')
initial_sock_colors = sock_bitmap.getcolors()

# convert sock into a numpy array
original_sock = np.array(sock_bitmap)

# method to switch one color c1 in the numpy_pixel_data to a different color c2
def convert_color(c1, c2, numpy_sock_pixel_data):
    # change color c1 to color c2
    (red, green, blue) = numpy_sock_pixel_data[:, :, 0], numpy_sock_pixel_data[:, :, 1], numpy_sock_pixel_data[:, :, 2]
    mask = (red == c1[0]) & (green == c1[1]) & (blue == c1[2])
    numpy_sock_pixel_data[:, :, :3][mask] = [c2[0], c2[1], c2[2]]
    return numpy_sock_pixel_data

def hex_to_rgb(hex):
    hex = hex.lstrip('#')
    hlen = len(hex)
    return tuple(int(hex[i:i + int(hlen / 3)], 16) for i in range(0, hlen, int(hlen / 3)))

colors_array = []
new_sock = original_sock.copy()

for c in initial_sock_colors:
    # print colors_array
    rindex = random.randint(0, 36)
    if rindex not in colors_array:
        colors_array.append(rindex)
        if not (c == (255, 255, 255)):
            new_sock = convert_color(c[1], hex_to_rgb(YARN_COLORS[rindex]), new_sock)

new_sock_bitmap = Image.fromarray(new_sock)
new_sock_bitmap
Out[31]:
In [42]:
# Now it's easy to make 10 new sock designs

colors_array = []
socks_array = []
new_sock = original_sock.copy()

for i in range(10):
    for c in initial_sock_colors:
        rindex = random.randint(0, 36)
        if rindex not in colors_array:
            colors_array.append(rindex)
            if not (c == (255, 255, 255)):
                new_sock = convert_color(c[1], hex_to_rgb(YARN_COLORS[rindex]), new_sock)
    new_sock_bitmap = Image.fromarray(new_sock)
    socks_array.append(new_sock_bitmap)
    new_sock = original_sock.copy()
    
socks_array[0]
Out[42]:
In [44]:
socks_array[1]
Out[44]:

Next we'll upload these new designs to S3 so we can see hundreds at once but that's for another blog post.

This is just one of the many ways that Sock Club is trying to change the way that people think about designs. We want to create tools and software that help designers do their job more effectively as well as show them new options they may not have considered. All of this boils down to building a better product for you and giving our team the resources to make better design decisions.

Sock Club makes an excellent Father's Day gift Sign Up Here.

Here's a Membership Certificate you can give on Father's Day.

We also make custom socks for startups learn more here