Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_colorful_northern_lights.py
- import tkinter as tk
- from PIL import Image, ImageTk, ImageDraw, ImageFont, ImageFilter, ImageEnhance, ImageChops
- import math
- from itertools import combinations
- WW, HH = 640, 640
- cx, cy = WW // 2 - 1, HH // 2 - 1
- w, h = WW * 2, WW * 2
- zoom_factor = 0.9
- sz = 59
- root = tk.Tk()
- root.geometry('+0+0')
- canvas = tk.Canvas(root, width=WW, height=HH)
- canvas.pack()
- base = {
- (0, 0): (255, 255, 255), (0, 1): (255, 255, 0), (0, 2): (0, 255, 0),
- (1, 0): (255, 128, 0), (1, 1): (128, 128, 128), (1, 2): (0, 0, 255),
- (2, 0): (255, 0, 0), (2, 1): (128, 0, 128), (2, 2): (0, 0, 0)
- }
- small_img = Image.new('RGB', (3, 3))
- for y in range(3):
- for x in range(3):
- small_img.putpixel((x, y), base[(x, y)])
- base_img = init_img = small_img.resize((WW, HH), Image.LANCZOS)
- colors = [(255, 192, 203), (165, 42, 42), (0, 255, 255)] # Pink, Brown, Cyan
- colors = list(base.values()) + colors
- pairs = list(combinations(colors, 2))
- combos = []
- used_pairs = set()
- current_pair = pairs.pop(0)
- combos.extend(current_pair)
- used_pairs.add(current_pair)
- while pairs:
- last_color = combos[-1]
- next_pair = None
- for p in pairs:
- if p not in used_pairs:
- if p[0] == last_color:
- next_pair = p
- rev = False
- break
- elif p[1] == last_color:
- next_pair = p
- rev = True
- break
- if next_pair:
- pairs.remove(next_pair)
- used_pairs.add(next_pair)
- if rev:
- combos.append(next_pair[0])
- else:
- combos.append(next_pair[1])
- else:
- p = pairs.pop(0)
- used_pairs.add(p)
- combos.extend(p)
- gradient_width = 80
- total_width = len(combos) * gradient_width
- total_height = 100
- gradient_img = Image.new("RGB", (len(combos), 1))
- for i, color in enumerate(combos):
- gradient_img.putpixel((i, 0), color)
- gradient_img = gradient_img.resize((total_width, 1), resample=Image.LANCZOS)
- colors_data = list(gradient_img.getdata())
- mid4_cache = [(cx + px, cy + py) for (px, py) in [(0, 0), (0, 1), (1, 0), (1, 1)]]
- new_sz = int(WW * zoom_factor)
- TL = (WW - new_sz) // 2
- BR = TL + new_sz
- photo = ImageTk.PhotoImage(base_img)
- canvas.create_image(0, 0, anchor=tk.NW, image=photo)
- while 1:
- base_img = base_img.crop((TL, TL, BR, BR))
- base_img = base_img.resize((WW, HH), Image.LANCZOS)
- px = base_img.load()
- rgb = colors_data.pop(0)
- colors_data.append(rgb)
- for x, y in mid4_cache:
- px[x, y] = rgb
- diff = ImageChops.difference(base_img, init_img)
- fractal_image = ImageChops.invert(diff)
- photo.paste(fractal_image)
- root.update()
Advertisement
Add Comment
Please, Sign In to add comment