Skip to content

Instantly share code, notes, and snippets.

@chaitan94
Last active July 11, 2021 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaitan94/cb5fe8180678cd5048295ffaa2d07c64 to your computer and use it in GitHub Desktop.
Save chaitan94/cb5fe8180678cd5048295ffaa2d07c64 to your computer and use it in GitHub Desktop.
AI Programming - Generate Polka Dot Patterns

This code was mostly just me experimenting with Github Copilot. There is no other objective here, in case you were looking for one.

polka_dot_v1.py file contains code which was almost entirely generated by Copilot

polka_dot_v2.py file contains code where I manually broke the problem into multiple functions but the implentations of all the function was still left to Copilot for the most part

I tweeted more details about this code on my twitter: https://twitter.com/chaitan94/status/1414275927299686402?s=20

from PIL import Image, ImageDraw
import random
# Procedurally generate a anonymized user avatar using PIL
# Background color palette will be randomly chosen to have a handful of mild, natural, monochromatic colors
# Background pattern will be a reptitive polka dot pattern
# Avatar is generated in a square shape of size 64x64
# Finally avatar will be stored with the provided filename as a PNG
def generate_random_avatar(seed, filename):
random.seed(seed)
background_colors = [
(255, 255, 255),
(255, 255, 0),
(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
]
background_pattern = [(0, 0, 0), (255, 255, 255), (255, 255, 255)]
background_color = random.choice(background_colors)
background_pattern = random.choice(background_pattern)
im = Image.new("RGB", (64, 64), background_color)
draw = ImageDraw.Draw(im)
# Draw background pattern
for i in range(0, 64, 8):
for j in range(0, 64, 8):
draw.point((i, j), background_pattern)
# Draw random polka dots
for i in range(0, 64, 4):
for j in range(0, 64, 4):
dot_color = random.randint(0, 255)
dot_size = random.randint(2, 8)
draw.point((i, j), (dot_color, dot_color, dot_color))
draw.point((i + 1, j), (dot_color, dot_color, dot_color))
draw.point((i, j + 1), (dot_color, dot_color, dot_color))
draw.point((i + 1, j + 1), (dot_color, dot_color, dot_color))
im.save(filename + ".png", "PNG")
if __name__ == "__main__":
# Generate random avatar
for i in range(10):
generate_random_avatar(random.randint(0, 100), "out/avatar-{}".format(i))
from PIL import Image, ImageDraw
import random
def rgb_to_hsv(r, g, b):
r, g, b = r / 255.0, g / 255.0, b / 255.0
mx = max(r, g, b)
mn = min(r, g, b)
df = mx - mn
if mx == mn:
h = 0
elif mx == r:
h = (60 * ((g - b) / df) + 360) % 360
elif mx == g:
h = (60 * ((b - r) / df) + 120) % 360
elif mx == b:
h = (60 * ((r - g) / df) + 240) % 360
if mx == 0:
s = 0
else:
s = df / mx
v = mx
return h, s, v
def hsv_to_rgb(h, s, v):
h_i = int(h * 6)
f = h * 6 - h_i
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
if h_i == 0:
r, g, b = v, t, p
elif h_i == 1:
r, g, b = q, v, p
elif h_i == 2:
r, g, b = p, v, t
elif h_i == 3:
r, g, b = p, q, v
elif h_i == 4:
r, g, b = t, p, v
elif h_i == 5:
r, g, b = v, p, q
return int(r * 255), int(g * 255), int(b * 255)
# Procedurally generate a randomized color.
# The color should have mild saturation, low lightness and completely random hue.
def generate_mild_color(seed, hue=None):
# Randomize the seed
random.seed(seed)
# Randomize the hue
if hue is None:
hue = random.randint(0, 360)
# Randomize the saturation
saturation = random.randint(50, 100)
# Randomize the lightness
lightness = random.randint(20, 60)
# Convert the hue to a rgb value
r, g, b = hsv_to_rgb(hue / 360.0, saturation / 100.0, lightness / 100.0)
# Return the color
return r, g, b
# Generate a brighter and more saturated color based on a lighter color
def brighten_color(color):
r, g, b = color
return int(r * 2), int(g * 2), int(b * 2)
# Procedurally generate a randomized polka dot pattern using PIL
# The background color (bg_color) will be generated using generate_mild_color with a random hue
# Background pattern will be a repetitive/tiled polka dots pattern (circles)
# The color of polka dots pattern (pdp_color) should based on a brighter version of the background color. i.e, pdp_color = brighten_color(bg_color)
# The final image is generated in a square shape of size 64x64 and will be stored with the provided filename as a PNG
def generate_polka_dot(seed, filename):
# Create a background color based on a random seed
bg_color = generate_mild_color(seed)
# Create a polka dots pattern color based on a lighter version of the background color
pdp_color = brighten_color(bg_color)
# Create a square image of size 64x64
img = Image.new("RGB", (64, 64), bg_color)
# Create a draw object to be used for drawing on the image
draw = ImageDraw.Draw(img)
# Create a 8x8 polka dot pattern within a square of size 64x64
# Alternative rows should be horizontally offset from each other
# The dots should have some spacing in between them
for x in range(0, 64, 8):
for y in range(0, 64, 8):
spacing = 4
radius = 8 - spacing
x_offset = 0 if (y // 8) % 2 else 4
draw.ellipse(
(x + x_offset, y, x + x_offset + radius, y + radius), pdp_color
)
# Save the image to disk
img.save(filename)
if __name__ == "__main__":
for i in range(16):
generate_polka_dot(random.random(), "out/polka-dots-{}.png".format(i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment