Python Pillow Art: Create a Mandelbrot Zoom Animation

andreasPython Code2 weeks ago37 Views

Skill level: Beginner → Intermediate
What you’ll learn:

  • Basics of fractals
  • Pixel-by-pixel image generation with Pillow
  • Loops and simple math to map coordinates to colors
  • Optional: Export as GIF for animations
from PIL import Image

# Image size (pixels)
WIDTH = 800
HEIGHT = 600

# Plot window
RE_START = -2
RE_END = 1
IM_START = -1
IM_END = 1

MAX_ITER = 100

def mandelbrot(c):
    z = 0
    n = 0
    while abs(z) <= 2 and n < MAX_ITER:
        z = z*z + c
        n += 1
    return n

img = Image.new('RGB', (WIDTH, HEIGHT))
pixels = img.load()

for x in range(WIDTH):
    for y in range(HEIGHT):
        re = RE_START + (x / WIDTH) * (RE_END - RE_START)
        im = IM_START + (y / HEIGHT) * (IM_END - IM_START)
        c = complex(re, im)
        color = mandelbrot(c)
        pixels[x, y] = (color % 8 * 32, color % 16 * 16, color % 32 * 8)

img.save('mandelbrot.png')
print("Saved mandelbrot.png")

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Leave a reply

Loading Next Post...
Loading

Signing-in 3 seconds...