here2share

# Tk_perlin_scroll.py

Apr 7th, 2022 (edited)
1,194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. # Tk_perlin_scroll.py
  2.  
  3. from tkinter import *
  4. from PIL import Image, ImageTk
  5. import random
  6. ri = random.randint
  7.  
  8. ww = 600
  9. hh = 600
  10.  
  11. random.seed(0)
  12.  
  13. def rgb2hex(r,g,b):
  14.     return '#%02X%02X%02X'%(r,g,b)
  15.  
  16. def draw():
  17.     image.putdata(rgb)
  18.     photo = ImageTk.PhotoImage(image)
  19.     canvas.create_image(0,0,image=photo,anchor=NW)
  20.     canvas.update()
  21.  
  22. t = [16,8,7,6,5,5,4,4,4,3,3,3,3,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1]
  23. ttt = []
  24. i = 0
  25. for i in range(1,25):
  26.     for j in range(t[i]):
  27.         ttt += [i]
  28.  
  29. v255 = [i for i in range(256)]
  30. v255 = v255[1:-1]+v255[::-1]
  31. rgbL = len(v255)
  32. o255 = {}
  33. e = len(ttt)-1
  34. for i in range(256):
  35.     o255[i] = [0]
  36.     j = 0
  37.     while sum(o255[i]) <= i/2:
  38.         o255[i] += [ttt[j]]
  39.         j += 1
  40.     while sum(o255[i])*2 > i:
  41.         o255[i].remove(1)
  42.     o255[i] += o255[i][::-1]
  43.     if sum(o255[i]) < i:
  44.         o255[i].insert(1,1)
  45.     o255[256+i] = [-z for z in o255[i]]
  46. oL = len(o255)
  47. PALETTE = list(o255.keys())*2
  48. # print (o255,oL)
  49.  
  50. def palette():
  51.     s = PALETTE.pop((i**j)%oL)
  52.     PALETTE.append(s)
  53.     return s
  54.    
  55.  
  56.  
  57. root = Tk()
  58. root.title("Tk_perlin_scroll.py")
  59. root.geometry("%dx%d+0+0"%(ww,hh))
  60.    
  61. canvas = Canvas(root, width=ww, height=hh)
  62. canvas.pack()
  63.  
  64. image = Image.new("RGB", (ww,hh), (255,255,255))
  65.  
  66. scan = [(128,128,128) for i in '.'*ww]
  67.  
  68. rgb = []
  69. i = 0
  70. __RGB = [[],[],[]]
  71. while len(rgb) < ww*hh:
  72.     for j in range(ww):
  73.         t = []
  74.         for k in (0,1,2):
  75.             if not __RGB[k]:
  76.                 s = palette()
  77.                 i += 1
  78.                 __RGB[k] = o255[s][:]
  79.             t += [v255[(scan[j][k]+__RGB[k].pop()*5)%rgbL]] #
  80.         scan[j] = tuple(t)
  81.     rgb.extend(scan)
  82.     draw()
  83.  
  84. while 1:
  85.     rgb = rgb[ww:]
  86.     for j in range(ww):
  87.         t = []
  88.         for k in (0,1,2):
  89.             if not __RGB[k]:
  90.                 s = palette()
  91.                 i += 1
  92.                 __RGB[k] = o255[s][:]
  93.             t += [v255[(scan[j][k]+__RGB[k].pop()*5)%rgbL]] #
  94.         scan[j] = tuple(t)
  95.     rgb.extend(scan)
  96.     draw()
Add Comment
Please, Sign In to add comment