here2share

# tk_colorful_northern_lights.py

Jul 20th, 2026
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. # tk_colorful_northern_lights.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageDraw, ImageFont, ImageFilter, ImageEnhance, ImageChops
  5. import math
  6. from itertools import combinations
  7.  
  8. WW, HH = 640, 640
  9. cx, cy = WW // 2 - 1, HH // 2 - 1
  10. w, h = WW * 2, WW * 2
  11. zoom_factor = 0.9
  12. sz = 59
  13.  
  14. root = tk.Tk()
  15. root.geometry('+0+0')
  16. canvas = tk.Canvas(root, width=WW, height=HH)
  17. canvas.pack()
  18.  
  19. base = {
  20.     (0, 0): (255, 255, 255), (0, 1): (255, 255, 0), (0, 2): (0, 255, 0),
  21.     (1, 0): (255, 128, 0),   (1, 1): (128, 128, 128), (1, 2): (0, 0, 255),
  22.     (2, 0): (255, 0, 0),     (2, 1): (128, 0, 128), (2, 2): (0, 0, 0)
  23. }
  24.  
  25. small_img = Image.new('RGB', (3, 3))
  26. for y in range(3):
  27.     for x in range(3):
  28.         small_img.putpixel((x, y), base[(x, y)])
  29.  
  30. base_img = init_img = small_img.resize((WW, HH), Image.LANCZOS)
  31.  
  32. colors = [(255, 192, 203), (165, 42, 42), (0, 255, 255)] # Pink, Brown, Cyan
  33. colors = list(base.values()) + colors
  34.  
  35. pairs = list(combinations(colors, 2))
  36. combos = []
  37. used_pairs = set()
  38.  
  39. current_pair = pairs.pop(0)
  40. combos.extend(current_pair)
  41. used_pairs.add(current_pair)
  42.  
  43. while pairs:
  44.     last_color = combos[-1]
  45.     next_pair = None
  46.     for p in pairs:
  47.         if p not in used_pairs:
  48.             if p[0] == last_color:
  49.                 next_pair = p
  50.                 rev = False
  51.                 break
  52.             elif p[1] == last_color:
  53.                 next_pair = p
  54.                 rev = True
  55.                 break
  56.     if next_pair:
  57.         pairs.remove(next_pair)
  58.         used_pairs.add(next_pair)
  59.         if rev:
  60.             combos.append(next_pair[0])
  61.         else:
  62.             combos.append(next_pair[1])
  63.     else:
  64.         p = pairs.pop(0)
  65.         used_pairs.add(p)
  66.         combos.extend(p)
  67.  
  68. gradient_width = 80
  69. total_width = len(combos) * gradient_width
  70. total_height = 100
  71.  
  72. gradient_img = Image.new("RGB", (len(combos), 1))
  73. for i, color in enumerate(combos):
  74.     gradient_img.putpixel((i, 0), color)
  75. gradient_img = gradient_img.resize((total_width, 1), resample=Image.LANCZOS)
  76. colors_data = list(gradient_img.getdata())
  77.  
  78. mid4_cache = [(cx + px, cy + py) for (px, py) in [(0, 0), (0, 1), (1, 0), (1, 1)]]
  79.  
  80. new_sz = int(WW * zoom_factor)
  81. TL = (WW - new_sz) // 2
  82. BR = TL + new_sz
  83.  
  84. photo = ImageTk.PhotoImage(base_img)
  85. canvas.create_image(0, 0, anchor=tk.NW, image=photo)
  86.  
  87. while 1:
  88.     base_img = base_img.crop((TL, TL, BR, BR))
  89.     base_img = base_img.resize((WW, HH), Image.LANCZOS)
  90.    
  91.     px = base_img.load()
  92.     rgb = colors_data.pop(0)
  93.     colors_data.append(rgb)
  94.    
  95.     for x, y in mid4_cache:
  96.         px[x, y] = rgb
  97.        
  98.     diff = ImageChops.difference(base_img, init_img)
  99.     fractal_image = ImageChops.invert(diff)
  100.    
  101.     photo.paste(fractal_image)
  102.     root.update()
Advertisement
Add Comment
Please, Sign In to add comment