Advertisement
here2share

# tk_layered_contours.py

Jun 3rd, 2023
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. # tk_layered_contours.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("layered_contours")
  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. Lc = len(colors)
  27.  
  28. def layered_contours():
  29.     intensity = 20
  30.     pixels = img2.load()
  31.     for y in range(hh):
  32.         for x in range(ww):
  33.             r,g,b = pixels[x,y]
  34.             r,g,b = [i//intensity*intensity for i in (r,g,b)]
  35.             pixels[x,y] = (r,g,b)
  36.  
  37. sz = 64
  38. i = 0
  39. xy = range(0, 512, sz)
  40. while 1:
  41.     for y in xy:
  42.         i = (i + 1) % 11
  43.         for x in xy:
  44.             color = colors.pop(i)
  45.             colors.append(color)
  46.             draw.rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
  47.             i = (i + 1) % 11
  48.  
  49.     img2 = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  50.     layered_contours()
  51.     tkimg = ImageTk.PhotoImage(img2)
  52.     canvas.create_image((cx, cy), image=tkimg)
  53.     canvas.update()
  54.  
  55. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement