Skip to content

Instantly share code, notes, and snippets.

@blakesanie
Last active April 4, 2024 00:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save blakesanie/ac7928c38764f7d9cc04b7f1db096d8e to your computer and use it in GitHub Desktop.
Save blakesanie/ac7928c38764f7d9cc04b7f1db096d8e to your computer and use it in GitHub Desktop.
from PIL import Image
import colorsys
import math
import os
#frame parameters
width = 1000 #pixels
x = -0.65
y = 0
xRange = 3.4
aspectRatio = 4/3
precision = 500
height = round(width / aspectRatio)
yRange = xRange / aspectRatio
minX = x - xRange / 2
maxX = x + xRange / 2
minY = y - yRange / 2
maxY = y + yRange / 2
img = Image.new('RGB', (width, height), color = 'black')
pixels = img.load()
def logColor(distance, base, const, scale):
color = -1 * math.log(distance, base)
rgb = colorsys.hsv_to_rgb(const + scale * color,0.8,0.9)
return tuple(round(i * 255) for i in rgb)
def powerColor(distance, exp, const, scale):
color = distance**exp
rgb = colorsys.hsv_to_rgb(const + scale * color,1 - 0.6 * color,0.9)
return tuple(round(i * 255) for i in rgb)
for row in range(height):
for col in range(width):
x = minX + col * xRange / width
y = maxY - row * yRange / height
oldX = x
oldY = y
for i in range(precision + 1):
a = x*x - y*y #real component of z^2
b = 2 * x * y #imaginary component of z^2
x = a + oldX #real component of new z
y = b + oldY #imaginary component of new z
if x*x + y*y > 4:
break
if i < precision:
distance = (i + 1) / (precision + 1)
rgb = powerColor(distance, 0.2, 0.27, 1.0)
pixels[col,row] = rgb
index = row * width + col + 1
print("{} / {}, {}%".format(index, width * height, round(index / width / height * 100 * 10) / 10))
img.save('output.png')
os.system('open output.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment