Created
August 18, 2022 14:25
-
-
Save hop/0f6f8d8a48d443daf2ea4c419d18b386 to your computer and use it in GitHub Desktop.
unfinished example of how to render to a tkinter PhotoImage, pixel by pixel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter as tk | |
WIDTH, HEIGHT = 512, 512 | |
TOP, LEFT = 1.5, -2.0 | |
BOTTOM, RIGHT = -1.5, 1.0 | |
STEPS = 256 | |
def mandelbrot(x, y): | |
a = b = 0 | |
for i in range(STEPS): | |
a, b = a**2 - b**2 + x, 2 * a * b + y | |
if a**2 + b**2 >= 4: | |
break | |
return (i + 1) % STEPS | |
class App(tk.Tk): | |
x = 0 | |
y = 0 | |
def __init__(self): | |
super().__init__() | |
self.title = 'Live Update PhotoImage Demo' | |
self.img = tk.PhotoImage(width=WIDTH, height=HEIGHT) | |
self.after(10, self.render) | |
self.canvas = tk.Canvas(self, width=WIDTH, height=HEIGHT, bg="blue") | |
self.canvas.create_image(WIDTH, HEIGHT, image=self.img, anchor=tk.SE) | |
self.canvas.pack() | |
def render(self): | |
v = mandelbrot( | |
LEFT + (RIGHT - LEFT) / WIDTH * self.x, | |
TOP + (BOTTOM - TOP) / HEIGHT * self.y | |
) | |
v = pow(v, 3) % 256 | |
self.img.put(f'#{v:02x}{v:02x}{v:02x}', (self.x, self.y)) | |
self.x = (self.x + 1) % WIDTH | |
if self.x == 0: | |
self.y = (self.y + 1) % HEIGHT | |
if not self.x == self.y == 0: | |
self.after(1, self.render) | |
if __name__ == "__main__": | |
app = App() | |
app.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment