Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_flood_zoom.py
- import tkinter as tk
- from PIL import Image, ImageTk
- import math
- import random
- WW, HH = 640, 640
- CX, CY = WW // 2, HH // 2
- rnd = 256 / 15
- root = tk.Tk()
- root.title("# tk_flood_zoom.py")
- canvas = tk.Canvas(root, width=WW, height=HH)
- root.geometry("+10+10")
- canvas.pack()
- img = Image.new('RGB', (WW, HH), "black")
- rgb_list = {}
- rgb_list_max = {}
- step = 15
- max_value = 1000
- for r in range(0, 256, step):
- for g in range(0, 256, step):
- for b in range(0, 256, step):
- rgb_list[(r, g, b)] = max_value
- Lrgb = len(rgb_list)
- rgb_list_max = rgb_list.copy()
- rgb_cue = list(rgb_list)
- def interpolate_color(color1, color2, factor):
- return tuple(int(color1[i] + (color2[i] - color1[i]) * factor) for i in range(3))
- grid_colors = {
- (0, 0): (255, 255, 255),
- (0, 1): (255, 255, 0),
- (0, 2): (0, 255, 0),
- (1, 0): (255, 165, 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)
- }
- pixel_data = []
- cXY = []
- inner = {}
- for j in range(HH):
- for i in range(WW):
- x = i / (WW - 1) * 2
- y = j / (HH - 1) * 2
- x0, y0 = int(x), int(y)
- x1, y1 = min(x0 + 1, 2), min(y0 + 1, 2)
- fx, fy = x - x0, y - y0
- color_top = interpolate_color(grid_colors[(x0, y0)], grid_colors[(x1, y0)], fx)
- color_bottom = interpolate_color(grid_colors[(x0, y1)], grid_colors[(x1, y1)], fx)
- color = interpolate_color(color_top, color_bottom, fy)
- pixel_data.append(color)
- distance = ((CX - i) ** 2 + (CY - j) ** 2) ** 0.5
- xy2 = math.atan2(i - CX, j - CY)
- if distance < 60:
- cXY.append(((int(distance), xy2), i, j))
- if distance < 2:
- inner[(i, j)] = 0
- cXY.sort()
- cXY = [(x, y) for z, x, y in cXY]
- LXY = len(cXY)
- img.putdata(pixel_data)
- def call_rgb(rgb):
- rgb = tuple([(value + 1) // step * step for value in rgb])
- return rgb_list[rgb], rgb
- def plot(i):
- for j in range(i, LXY, n * 2):
- x, y = cXY[j]
- if (x, y) in inner:
- rgb = random.choice(rgb_cue)
- else:
- neighbors = [img.getpixel((x + x0, y + y0)) for x0 in (-1, 1) for y0 in (-1, 1)]
- value, rgb = min([call_rgb(rgb) for rgb in neighbors])
- if value > 0:
- value = value - 1
- else:
- rgb_list_max[rgb] += 5
- value = rgb_list_max[rgb]
- rgb_list[rgb] = value
- img.putpixel((x, y), rgb)
- scale = 20
- n = 6
- oXY = [i if i % 2 else n * 2 - i - 2 for i in range(n * 2)]
- while True:
- for i in oXY:
- canvas.delete('all')
- img = img.crop((scale, scale, WW - scale, HH - scale))
- img = img.resize((WW, HH))
- photo = ImageTk.PhotoImage(img)
- canvas.create_image(0, 0, image=photo, anchor=tk.NW)
- root.update()
- plot(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement