# https://realpython.com/mandelbrot-set-python/ from dataclasses import dataclass @dataclass class MandelbrotSet: max_iterations: int def __contains__(self, c: complex) -> bool: return self.stability(c) == 1 def stability(self, c: complex) -> float: return self.escape_count(c) / self.max_iterations def escape_count(self, c: complex) -> int: z = 0 for iteration in range(self.max_iterations): z = z**2 + c if abs(z) > 2: return iteration return self.max_iterations mandelbrot_set = MandelbrotSet(max_iterations=20) width, height = 512, 512 scale = 0.0075 GRAYSCALE = "L" from PIL import Image image = Image.new(mode=GRAYSCALE, size=(width, height)) for y in range(height): for x in range(width): c = scale * complex(x - width / 2, height / 2 - y) instability = 1 - mandelbrot_set.stability(c) image.putpixel((x, y), int(instability * 255)) image.show()