-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjuliaset.py
71 lines (64 loc) · 2.97 KB
/
juliaset.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
from PIL import Image, ImageDraw
from math import sqrt
from datetime import datetime
from random import randint
import os
class JuliaSet:
def __init__(self, ca, cb, w, h, c_mode, date_img):
self.ca = ca
self.cb = cb
self.w = w
self.h = h
self.c_mode = c_mode
self.date_img = date_img
self.image = Image.new("HSV" if c_mode == "multi_color" else "RGB",(w,h))
self.draw = ImageDraw.Draw(self.image)
self.colorbias = (randint(20,200), randint(20,200), randint(20,200))
self.glow = (randint(0,20), randint(0,20), randint(0,20))
self.t = datetime.now()
self.minutes = self.t.minute if len(str(self.t.minute)) != 1 else "0" + str(self.t.minute)
self.date_stamp = f"{self.t.month}/{self.t.day}/{self.t.year}"
self.time_stamp = f"{self.t.hour}:{self.minutes}"
def genImage(self, iter_per_pixel, zoom):
self.max_iter = iter_per_pixel
for x in range(self.w):
for y in range(self.h):
za = self.translate(x,0,self.w,-zoom,zoom)
zb = self.translate(y,0,self.h,-zoom,zoom)
i = 0
while i < self.max_iter:
tmp = 2 * za * zb
za = za * za - zb * zb + self.ca
zb = tmp + self.cb
if sqrt(za*za + zb*zb) > 4: break
i += 1
self.draw.point((x,y), self.colorize(i) if i != self.max_iter else (0,0,0))
def saveImage(self,path):
self.genName()
print(f"{self.date_stamp} | {self.time_stamp} SAVED: {self.file_name}\nMODE: {self.c_mode}\nITERATIONS: {self.max_iter}")
if path != "saves" and not os.path.exists(path):
os.mkdir(path)
elif not os.path.exists("saves"):
os.mkdir("saves")
self.image = self.image.convert(mode="RGB")
self.image.save(f"{path}/{self.file_name}","PNG")
def genName(self):
if self.date_img:
self.file_name = f"image_{self.t.month}{self.t.day}{self.t.year}_{self.t.hour}{self.minutes}{self.t.second}.png"
else:
self.file_name = "image.png"
def colorize(self, i):
if self.c_mode == "rand_color":
c = int(self.translate(i,0,self.max_iter,0,255) * (i/4))
return (c + self.colorbias[0], c + self.colorbias[1], c + self.colorbias[2])
elif self.c_mode == "rand_pattern":
return self.colorbias if i % 2 == 0 else (0,0,0)
elif self.c_mode == "rand_glow":
return (i*self.glow[0], i*self.glow[1], i*self.glow[2])
elif self.c_mode == "multi_color":
return (int(200*i/self.max_iter) + self.colorbias[0], 200, 255 if i != self.max_iter else 0)
else:
print(f"Unknown color mode: '{self.c_mode}'")
exit()
def translate(self, value, leftMin, leftMax, rightMin, rightMax):
return rightMin + (float(value - leftMin) / float(leftMax - leftMin) * (rightMax - rightMin))