Advertisement
here2share

# tk_caustic_lines.py

Jul 19th, 2023
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. # tk_caustic_lines.py
  2.  
  3. from tkinter import *
  4. from PIL import Image, ImageTk, ImageFilter, ImageDraw, ImageEnhance
  5. import copy
  6.  
  7. ww = 256
  8. hh = 256
  9. cx, cy = ww//2, hh//2
  10.  
  11. def rgb2hex(r,g,b):
  12.     return '#%02X%02X%02X'%(r,g,b)
  13.  
  14. rgb = range(0, 256, 10)
  15. colors = [rgb2hex(z, z, z) for z in rgb]
  16.  
  17. img = Image.new('RGB', (ww, hh), (0, 0, 0))
  18. draw = {}
  19. draw[0] = ImageDraw.Draw(img)
  20. blur_radius = 0.03 * min(img.size)
  21.  
  22. def display(new_img):
  23.     tkimg = ImageTk.PhotoImage(new_img)
  24.     canvas.create_image((cx, cy), image=tkimg)
  25.     canvas.update()
  26.  
  27. root = Tk()
  28. root.title("tk_caustic_lines")
  29. root.geometry("%dx%d+10+10"%(ww,hh))
  30. canvas = Canvas(root, width=ww, height=hh, bg='white')
  31. canvas.pack(side=LEFT, fill=BOTH, expand=True)
  32.  
  33. sz = 16
  34. span = ww//sz
  35. c = 0
  36. xy = range(0, sz*span, sz)
  37.        
  38. sharpening_masks = []
  39.  
  40. while 1:
  41.     for y in xy:
  42.         c = (c + 2) % 11
  43.         for x in xy:
  44.             color = colors.pop(c)
  45.             colors.append(color)
  46.             draw[0].rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
  47.             c = (c + 3) % 11
  48.     draw['target'] = img.filter(ImageFilter.GaussianBlur(radius=blur_radius)).convert("L").point(lambda x: 0 if 120 < x < 128 else 255)
  49.     display(draw['target'])
  50.     if draw['target'] not in sharpening_masks[:1]:
  51.         sharpening_masks += [draw['target']]
  52.     else:
  53.         break
  54. print("grayscale masks created")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement