here2share

# tk_pil_point_permu.py

Jul 12th, 2026 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. # tk_pil_point_permu.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageDraw, ImageFont, ImageFilter, ImageEnhance
  5. import math
  6. from itertools import chain
  7.  
  8. WW, HH = 640, 640
  9. cx, cy = WW // 2, HH // 2
  10.  
  11. root = tk.Tk()
  12. root.geometry('+0+0')
  13. canvas = tk.Canvas(root, width=WW, height=HH)
  14. canvas.pack()
  15.  
  16. base = {
  17.     (0, 0): (255, 255, 255),   (0, 1): (255, 255,   0),   (0, 2): (0, 255,   0),
  18.     (1, 0): (255, 128,   0),   (1, 1): (128, 128, 128),   (1, 2): (0,   0, 255),
  19.     (2, 0): (255,   0,   0),   (2, 1): (128,   0, 128),   (2, 2): (0,   0,   0)
  20. }
  21. small_img = Image.new('RGB', (3, 3))
  22. for y in range(3):
  23.     for x in range(3):
  24.         small_img.putpixel((x, y), base[(x, y)])
  25.        
  26. resamplers = {
  27.     Image.NEAREST: "NEAREST",
  28.     Image.BOX: "BOX",
  29.     Image.BILINEAR: "BILINEAR",
  30.     Image.HAMMING: "HAMMING",
  31.     Image.BICUBIC: "BICUBIC",
  32.     Image.LANCZOS: "LANCZOS"
  33. }
  34.  
  35. best_resampler = 'none'
  36. val = 0
  37. for imgtype in resamplers:
  38.     resized = small_img.resize((WW, HH), imgtype)
  39.     data = list(resized.getdata())
  40.  
  41.     unique_colors = len(set(data))
  42.     if unique_colors > val:
  43.         val = unique_colors
  44.         best_resampler = imgtype
  45.     print(f"Method: {resamplers[imgtype]} | Unique Colors: {unique_colors}")
  46.  
  47. img = small_img.resize((WW, HH), best_resampler)
  48.  
  49. permutation_list = [i for i in range(256)]
  50.  
  51. def get_next_permutation(p):
  52.     p = list(p)
  53.     p.append(p.pop(0))
  54.     mid = len(p) // 2
  55.     return tuple(chain.from_iterable(zip(p[:mid], p[mid:])))
  56.  
  57. def func(i):
  58.     return permutation_list[i % 256]
  59.  
  60. variety = set()
  61. while 1:
  62.     photo = ImageTk.PhotoImage(img)
  63.     canvas.create_image(0, 0, anchor=tk.NW, image=photo)
  64.     root.update()
  65.  
  66.     img = img.point(lambda i: func(i))
  67.     permutation_list = get_next_permutation(permutation_list)
  68.  
  69.     if permutation_list in variety:
  70.         variety.add(permutation_list)
  71.         print(len(permutation_list))
  72.         break
Advertisement
Add Comment
Please, Sign In to add comment