Advertisement
here2share

# Tk_4x4_combos.py

Feb 9th, 2021 (edited)
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. # Tk_4x4_combos.py
  2.  
  3. from Tkinter import *
  4. import itertools
  5. import os
  6. from PIL import ImageGrab
  7. from PIL import ImageDraw
  8. from PIL import Image
  9.  
  10. # Number of squares in x and y dimensions.
  11. n = 4
  12.  
  13. # Total dimensions of display.
  14.  
  15. t = 64
  16. ww = t
  17. hh = t
  18.  
  19. '''
  20. directory = tkFileDialog.askdirectory()
  21. print (directory)
  22. '''
  23.  
  24. directory = "C:/py/artio"+"/"
  25.  
  26. root = Tk()
  27. root.geometry("%sx%s+0+0" % (ww, hh))
  28. canvas = Canvas(root, width=ww, height=hh)
  29. canvas.pack()
  30.  
  31. img_pil = Image.new("RGB", (ww, hh))
  32. cv_pil = ImageDraw.Draw(img_pil)
  33.  
  34. def pil_to_jpg(filename, img):
  35.     img.save(filename)
  36.  
  37. def combine(zeros, ones):
  38.     for indices in itertools.combinations(range(zeros+ones), ones):
  39.         item = ['0'] * (zeros+ones)
  40.         for index in indices:
  41.             item[index] = '1'
  42.         yield ''.join(item)
  43.  
  44. def make_square(x, y, color):
  45.     return canvas.create_rectangle(x * ss, y * ss, (x+1) * ss, (y+1) * ss,
  46.                               fill=color, outline=color)
  47.  
  48. def make_squares():
  49.     squares = []
  50.     for x in range(n):
  51.         squares.append([])
  52.         for y in range(n):
  53.             squares[x].append(make_square(x, y, 'white'))
  54.     return squares
  55.  
  56. done = False
  57. def quit_handler(event):
  58.     global done
  59.     done = True
  60.  
  61. # Left mouse button click on canvas terminates the program.
  62. root.bind('<Button-1>', quit_handler)
  63.  
  64. ss = ww/n
  65. squares = make_squares()
  66.  
  67. black = n*n/2
  68.  
  69. ppp = []
  70. while black < n*n+1:
  71.     white = n*n-black
  72.     combos = combine(white, black)
  73.     for combo in combos:
  74.         seq = combo
  75.         combo = list(seq)
  76.         for x in range(len(squares)):
  77.             for y in range(len(squares[x])):
  78.                 sq = squares[x][y]
  79.                 color = 'black' if int(combo.pop()) else 'white'
  80.                 canvas.itemconfig(sq, fill=color, outline=color)
  81.                 cv_pil.rectangle((x*ss,y*ss,x*ss+ss,y*ss+ss), fill=color, outline=color)
  82.         root.update()
  83.         filename = directory+seq+".jpg"
  84.         pil_to_jpg(filename, img_pil)
  85.     black += 1
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement