|
| 1 | +# Julia Fractal using only math and PIL libraries |
| 2 | +# the image is calculated and opened in the system's standard image viewer |
| 3 | +# as PNG file |
| 4 | +# PIL (Pillow) does not come with a standard CPython installation |
| 5 | +# see: https://pypi.org/project/pillow/ |
| 6 | +# |
| 7 | +from PIL import Image, ImageDraw |
| 8 | +from math import sqrt |
| 9 | +# parameters |
| 10 | +screen_width = 1600; screen_height = 1000 # image size in pixels |
| 11 | +# create a new PIL Image object |
| 12 | +img = Image.new("RGB",(screen_width, screen_height), |
| 13 | + "black") |
| 14 | +draw = ImageDraw.Draw(img) |
| 15 | +# calculation and plotting |
| 16 | +c = -0.4 + 0.6j # complex constant for julia fractal |
| 17 | +# other interesting values: |
| 18 | +# -0.5125 + 0.5213j, -0.499 + 0.5213j, -0.498 + 0.5213j, |
| 19 | +# -0.8 + 0.156, -0.7269 + 0.1889 |
| 20 | +for x in range(0, screen_width): |
| 21 | + re = x / (screen_width - 1) * 3.2 - 1.6 |
| 22 | + for y in range(0, screen_height): |
| 23 | + im = y / (screen_height - 1) * 2.0 - 1.0 |
| 24 | + z = complex(re, im) # initial value for z |
| 25 | + for i in range(1025): # counter will be measure for how fast z grows |
| 26 | + z = z**2 + c |
| 27 | + if abs(z) > 2.0: |
| 28 | + break |
| 29 | + i = int(sqrt(i)*8) # apply non linear scaling on i |
| 30 | + r = i % 33 * 8; r = min(255, r) # calculate color comp. from i |
| 31 | + g = i % 129 * 2; g = min(255, g) |
| 32 | + b = i % 65 * 4; b = min(255, b) |
| 33 | + col = (r,g,b) |
| 34 | + draw.point([x, y], fill = col) |
| 35 | + if x % 100 == 0: |
| 36 | + print(f"{x/(screen_width-1):.0%} calculated") |
| 37 | +# plotting finished, image opens in standard img viewer |
| 38 | +img.show() |
0 commit comments