Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_pil_point_permu.py
- import tkinter as tk
- from PIL import Image, ImageTk, ImageDraw, ImageFont, ImageFilter, ImageEnhance
- import math
- from itertools import chain
- WW, HH = 640, 640
- cx, cy = WW // 2, HH // 2
- root = tk.Tk()
- root.geometry('+0+0')
- canvas = tk.Canvas(root, width=WW, height=HH)
- canvas.pack()
- base = {
- (0, 0): (255, 255, 255), (0, 1): (255, 255, 0), (0, 2): (0, 255, 0),
- (1, 0): (255, 128, 0), (1, 1): (128, 128, 128), (1, 2): (0, 0, 255),
- (2, 0): (255, 0, 0), (2, 1): (128, 0, 128), (2, 2): (0, 0, 0)
- }
- small_img = Image.new('RGB', (3, 3))
- for y in range(3):
- for x in range(3):
- small_img.putpixel((x, y), base[(x, y)])
- resamplers = {
- Image.NEAREST: "NEAREST",
- Image.BOX: "BOX",
- Image.BILINEAR: "BILINEAR",
- Image.HAMMING: "HAMMING",
- Image.BICUBIC: "BICUBIC",
- Image.LANCZOS: "LANCZOS"
- }
- best_resampler = 'none'
- val = 0
- for imgtype in resamplers:
- resized = small_img.resize((WW, HH), imgtype)
- data = list(resized.getdata())
- unique_colors = len(set(data))
- if unique_colors > val:
- val = unique_colors
- best_resampler = imgtype
- print(f"Method: {resamplers[imgtype]} | Unique Colors: {unique_colors}")
- img = small_img.resize((WW, HH), best_resampler)
- permutation_list = [i for i in range(256)]
- def get_next_permutation(p):
- p = list(p)
- p.append(p.pop(0))
- mid = len(p) // 2
- return tuple(chain.from_iterable(zip(p[:mid], p[mid:])))
- def func(i):
- return permutation_list[i % 256]
- variety = set()
- while 1:
- photo = ImageTk.PhotoImage(img)
- canvas.create_image(0, 0, anchor=tk.NW, image=photo)
- root.update()
- img = img.point(lambda i: func(i))
- permutation_list = get_next_permutation(permutation_list)
- if permutation_list in variety:
- variety.add(permutation_list)
- print(len(permutation_list))
- break
Advertisement
Add Comment
Please, Sign In to add comment