here2share

# tk_native_contour.py

Jun 3rd, 2023
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. # tk_native_contour.py
  2.    
  3. from tkinter import *
  4. from PIL import Image, ImageTk, ImageFilter, ImageDraw
  5. import math
  6.  
  7. ww = 512
  8. hh = 512
  9. cx, cy = ww//2, hh//2
  10.  
  11. root = Tk()
  12. root.title("native_contour")
  13. root.geometry("%dx%d+0+0"%(ww,hh))
  14.  
  15. img = Image.new('RGB', (ww, hh), (0, 0, 0))
  16. draw = ImageDraw.Draw(img)
  17. blur_radius = 0.06 * min(img.size)
  18.  
  19. canvas = Canvas(root, width=ww, height=hh, bg='white')
  20. canvas.pack(side=LEFT, fill=BOTH, expand=True)
  21.  
  22. def rgb2hex(r,g,b):
  23.     return '#%02X%02X%02X'%(r,g,b)
  24. rgb = range(0, 256, 50)
  25. colors = [rgb2hex(r, g, b) for r in rgb for g in rgb for b in rgb]
  26. rgb = []
  27. def create_rgb():
  28.     t = list(range(0, 256, 50))
  29.     rrr = t[:]
  30.     ggg = t[:]
  31.     bbb = t[:]
  32.     r = g = b = 0
  33.     while 1:
  34.         for r in rrr:
  35.             for g in ggg:
  36.                 for b in bbb:
  37.                     if (r,g,b) not in rgb:
  38.                         rgb.append((r,g,b))
  39.                     else:
  40.                         return
  41.                 if bbb.index(b) == len(bbb) - 1:
  42.                     bbb = bbb[::-1]
  43.             if ggg.index(g) == len(ggg) - 1:
  44.                 ggg = ggg[::-1]
  45.         rrr = rrr[::-1]
  46. create_rgb()
  47. Lc = len(rgb)
  48.  
  49. def contour():
  50.     pixels = img2.load()
  51.     for y in range(hh):
  52.         for x in range(ww):
  53.             r,g,b = pixels[x,y]
  54.             r,g,b = rgb[(r+g+b)%Lc]
  55.             pixels[x,y] = (r,g,b)
  56.  
  57. sz = 64
  58. i = 0
  59. xy = range(0, 512, sz)
  60. while 1:
  61.     for y in xy:
  62.         i = (i + 1) % 11
  63.         for x in xy:
  64.             color = colors.pop(i)
  65.             colors.append(color)
  66.             draw.rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
  67.             i = (i + 1) % 11
  68.  
  69.     img2 = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  70.     contour()
  71.     tkimg = ImageTk.PhotoImage(img2)
  72.     canvas.create_image((cx, cy), image=tkimg)
  73.     canvas.update()
  74.  
  75. root.mainloop()
Add Comment
Please, Sign In to add comment