-
Notifications
You must be signed in to change notification settings - Fork 0
/
geodelines.py
84 lines (73 loc) · 2.8 KB
/
geodelines.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Gregory Kehne
# This program draws randomly generated lines that squiggle but maintain a
# roughly uniform distance from one another. This is inspired by the growth
# lines that appear in the cross-sections of geodes, and by the work of
# artist Sol LeWitt, specifically "Wall Drawing 797".
import math
import random
from PIL import Image, ImageDraw
# this is the squiggle generator!
# step<=gap
# i is the index of the line
# j is the 'height' index within the line
# lines[i][j]=(x-coord, y-coord)
def squiggles(height, width, step, gap):
# array to contain all relevant points
lines = []
# temp list to store the previous line
line = []
# how far over the lines start
seed = 500
# generate squiggly lines
for i in range(0, width, gap):
if i == 0: # generate the original line
rprev = 0.0
rprevprev = 0.0
rprevprevprev = 0.0
for j in range(0, height / step):
randomness = random.uniform(-4, 4) + random.uniform(-3, 3) +random.uniform(-2, 2)
randomness += .5 * rprev + 0.25 * rprevprev + 0.125 * rprevprevprev
line.append((seed + randomness, j * step))
rprevprevprev = rprevprev
rprevprev = rprev
rprev = randomness
seed = seed + randomness
else: # generate derivative lines
newline = []
for j in range(0, height / step):
poss = [] # x-value candidates to choose from
for d in range(- (gap / step) + 1, gap / step):
if j + d >= 0 and j + d < height / step: # in y-range
poss.append(line[j + d][0] + math.sqrt(gap ** 2 - (d * step) ** 2))
newline.append((max(poss) + random.uniform(-0.5, 0.5), j * step))
line = newline
lines.append(line)
return(lines)
# this is the main method.
# parameters are dimensions of screen, step within line, gap between lines,
# output file name
def main (height, width, step, gap, name):
# generate squiggles
lines = squiggles(height, width, step, gap)
# new image created
im = Image.new('RGBA', (height, width), (255, 255, 255, 0))
draw = ImageDraw.Draw(im)
# draw the lines
# i iterates horizontally across lines
for i in range(len(lines)):
drift = i / len(lines) # make function that drifts between colors.
color = (random.randint(int(0 + drift * (130)), int(60 + drift * 100)),
random.randint(int(100 - (drift * (100 - 32))),
int(190 - drift * (190 - 32))), random.randint(210, 220), 0)
# width for line
w = random.randint(3, 5)
# draw all the individual segments. Here j steps down the vertical axis
for j in range(len(lines[i]) - 1):
draw.line((lines[i][j][0], lines[i][j][1], lines[i][j + 1][0],
lines[i][j + 1][1]), fill=color, width=w)
# saves the generated Image
im.save(name + ".jpg")
# Default behavior: create an image 2000x2000 named geode_test.jpg with
# lines that take 4-pixel steps and sit 30 pixels apart.
if __name__ == '__main__':
main(2000, 2000, 4, 30, "geode_test")