## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

Chapter 22

# Image Synthesis

The Mandelbrot set is an interesting mathematical object that owes much of its popularity to the computer programs that create images of it.

Listing 22.1: Mandelbrot Set

` 1 # mandelbrot.py`
` 2 from image import ImagePPM`
` 3`
` 4 def lerp(frac, low, high):`
` 5 return low + frac * (high − low)`
` 6`
` 7 def testpoint(c, maxreps):`
` 8 z = 0`
` 9 reps = 0`
`10 while abs(z) < 2 and reps < maxreps:`
`11   z = z ** 2 + c`
`12   reps += 1`
`13 frac = reps / maxreps`
`14 return (0, 0, int(lerp(frac, 0, 255)))`
`15`
`16 def mandelbrot(xint, yint, size, maxreps):`
`17 width, height = size`
`18 img = ImagePPM.new(size)`
`19 for i in range(width):`
`20  for j in range(height):`
`21    a = lerp(i / width, xint[0], xint[1])`
`22    b = lerp(1 − j / height, yint[0], yint[1])`
`23 ...`

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required