Advertisement
here2share

# Tk_perlin_scroll_rnd.py

Apr 7th, 2022
1,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. # Tk_perlin_scroll_rnd.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.  
  51. root = Tk()
  52. root.title("Tk_perlin_scroll.py")
  53. root.geometry("%dx%d+0+0"%(ww,hh))
  54.    
  55. canvas = Canvas(root, width=ww, height=hh)
  56. canvas.pack()
  57.  
  58. image = Image.new("RGB", (ww,hh), (255,255,255))
  59.  
  60. scan = [(128,128,128) for i in '.'*ww]
  61.  
  62. rgb = []
  63. __RGB = [[],[],[]]
  64. while len(rgb) < ww*hh:
  65.     for j in range(ww):
  66.         t = []
  67.         for k in (0,1,2):
  68.             if not __RGB[k]:
  69.                 s = PALETTE.pop(ri(0,oL))
  70.                 PALETTE.append(s)
  71.                 __RGB[k] = o255[s][:]
  72.             t += [v255[(scan[j][k]+__RGB[k].pop()*5)%rgbL]] #
  73.         scan[j] = tuple(t)
  74.     rgb.extend(scan)
  75.     draw()
  76.  
  77. while 1:
  78.     rgb = rgb[ww:]
  79.     for j in range(ww):
  80.         t = []
  81.         for k in (0,1,2):
  82.             if not __RGB[k]:
  83.                 s = PALETTE.pop(i%oL)
  84.                 PALETTE.append(s)
  85.                 i += 1
  86.                 __RGB[k] = o255[s][:]
  87.             t += [v255[(scan[j][k]+__RGB[k].pop()*5)%rgbL]] #
  88.         scan[j] = tuple(t)
  89.     rgb.extend(scan)
  90.     draw()
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement