Advertisement
here2share

# tk_tight_contour_patterns.py

Nov 19th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. # tk_tight_contour_patterns.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageDraw, ImageFilter, ImageChops
  5. import random
  6.  
  7. ww = 640
  8. hh = 640
  9. p = 64
  10. cx = ww // 2
  11. cy = hh // 2
  12.  
  13. root = tk.Tk()
  14. root.title("Tight Contour Patterns")
  15. root.geometry(f"+-10+0")
  16. canvas = tk.Canvas(root, width=ww, height=hh)
  17. canvas.pack()
  18.  
  19. img = Image.new("L", (ww + p, hh + p), 255)
  20. contours = main_line = Image.new("RGBA", (ww + p, hh + p), (0, 0, 0, 0))
  21. transparent_img = Image.new("RGBA", img.size, (255, 255, 255, 0))
  22.  
  23. r = 300
  24. bg_circle = Image.new("RGBA", (ww + p, hh + p), (255, 255, 255, 255))
  25. draw = ImageDraw.Draw(bg_circle)
  26. draw.ellipse((ww // 2 - r + p // 2, hh // 2 - r + p // 2, ww // 2 + r + p // 2, hh // 2 + r + p // 2), fill=(0, 255, 0, 255))
  27.  
  28. fg_cache = [(x, y) for x in range(ww + p) for y in range(hh + p)]
  29.  
  30. def pil_perlin_noise(img):
  31.     draw = ImageDraw.Draw(img)
  32.     for y in range(0, hh + p, 32):
  33.         for x in range(0, ww + p, 32):
  34.             draw.rectangle((x, y, x + 32, y + 32), fill=random.choice([0, 255]))
  35.     img = img.filter(ImageFilter.GaussianBlur(25))
  36.     return img
  37.  
  38. def mask(img):
  39.     mask_img = Image.eval(img, lambda p: 255 if p == 255 else 0).convert("L")
  40.     return Image.composite(transparent_img, img, mask_img)
  41.  
  42. def polarize(img, contrast=128):
  43.     return img.point(lambda p: 255 if p < contrast else 0)
  44.  
  45. def thicken(main_line, blur=4):
  46.     main_line = main_line.filter(ImageFilter.GaussianBlur(blur))
  47.     main_line = polarize(main_line, contrast=250)
  48.     main_line = ImageChops.invert(main_line)
  49.     return main_line
  50.  
  51. def darken(p):
  52.     if p > 180:
  53.         return p
  54.     else:
  55.         return p * 0.7
  56.  
  57. def draw_contour(img):
  58.     img = polarize(img)
  59.     img = img.filter(ImageFilter.FIND_EDGES)
  60.     img = img.filter(ImageFilter.MaxFilter(3))
  61.     img = polarize(img)
  62.     return img
  63.  
  64. def display_pattern(img):
  65.     blur = img.filter(ImageFilter.GaussianBlur(2))
  66.     blur = blur.point(darken)
  67.     combined = Image.alpha_composite(bg_circle, blur)
  68.     tk_image = ImageTk.PhotoImage(combined)
  69.     canvas.create_image(cx, cy, anchor="center", image=tk_image)
  70.     canvas.tk_image = tk_image
  71.  
  72. def add_contour_lines():
  73.     global main_line, contours
  74.     main_line = thicken(main_line)
  75.     img = draw_contour(main_line)
  76.  
  77.     img = mask(img)
  78.     prev = contours
  79.     contours = Image.alpha_composite(contours, img)
  80.     if prev != contours:
  81.         add_contour_lines()
  82.     display_pattern(contours)
  83.  
  84. def create_pattern(event):
  85.     global main_line, contours
  86.     canvas.delete('all')
  87.     canvas.create_text(cx, cy, text="Processing Image...", anchor="center", fill='purple', font='ariel 40 bold')
  88.     canvas.update()
  89.     main_line = pil_perlin_noise(img)
  90.     main_line = draw_contour(main_line.convert('RGBA'))
  91.     contours = mask(main_line)
  92.     display_pattern(contours)
  93.     add_contour_lines()
  94.  
  95. root.bind("<space>", create_pattern)
  96. create_pattern(0)
  97.  
  98. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement