|
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)) |